C#隐藏控制台键盘输入的方法

 更新时间:2015年04月18日 13:58:53   作者:work24  
这篇文章主要介绍了C#隐藏控制台键盘输入的方法,涉及C#针对系统底层调用的相关技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了C#隐藏控制台键盘输入的方法。分享给大家供大家参考。具体如下:

using System;
namespace RobvanderWoude
{
 class HideInput
 {
  static int Main( string[] args )
  {
   try
   {
    bool clearscreen = false;
    if ( args.Length > 1 )
    {
     return WriteError( "Too many command line arguments" );
    }
    if ( args.Length == 1 )
    {
     switch ( args[0].ToUpper( ) )
     {
      case "/C":
       clearscreen = true;
       break;
      case "/?":
       return WriteError( );
      default:
       return WriteError( "Invalid command line argument \"" + args[0] + "\"" );
     }
    }
    // Set console foreground color to background color to hide what's being typed
    ConsoleColor color = Console.ForegroundColor;
    Console.ForegroundColor = Console.BackgroundColor;
    // Read 1 line of input from the console
    string input = Console.ReadLine( );
    // Restore the original console foreground color
    Console.ForegroundColor = color;
    // Clear the screen id specified on the command line
    if ( clearscreen )
    {
     Console.Clear( );
    }
    // Display the input - which should be redirected for this program to be of any use
    Console.WriteLine( input );
    // Returncode 0 for success, or 1 if the input was empty or whitespace only
    if ( string.IsNullOrWhiteSpace( input ) )
    {
     return 1;
    }
    else
    {
     return 0;
    }
   }
   catch ( Exception e )
   {
    return WriteError( e.Message );
   }
  }
  public static int WriteError( string errorMessage = "" )
  {
   Console.ResetColor( );
   if ( string.IsNullOrEmpty( errorMessage ) == false )
   {
    Console.Error.WriteLine( );
    Console.ForegroundColor = ConsoleColor.Red;
    Console.Error.Write( "ERROR: " );
    Console.ForegroundColor = ConsoleColor.White;
    Console.Error.WriteLine( errorMessage );
    Console.ResetColor( );
   }
   Console.Error.WriteLine( );
   Console.Error.WriteLine( "HideInput, Version 1.10" );
   Console.Error.WriteLine( "Batch utility to read 1 line of input while hiding what's being typed, by" );
   Console.Error.WriteLine( "temporarily setting the console foreground color equal to its background color" );
   Console.Error.WriteLine( );
   Console.Error.Write( "Usage: FOR /F \"tokens=*\" %%A IN ('" );
   Console.ForegroundColor = ConsoleColor.White;
   Console.Error.Write( "HIDEINPUT" );
   Console.ResetColor( );
   Console.Error.WriteLine( "') DO SET password=%%A" );
   Console.Error.Write( "  or: FOR /F \"tokens=*\" %%A IN ('" );
   Console.ForegroundColor = ConsoleColor.White;
   Console.Error.Write( "HIDEINPUT /C" );
   Console.ResetColor( );
   Console.Error.WriteLine( "') DO SET password=%%A" );
   Console.Error.WriteLine( );
   Console.Error.Write( "Where: " );
   Console.ForegroundColor = ConsoleColor.White;
   Console.Error.Write( "/C" );
   Console.ResetColor( );
   Console.Error.WriteLine( " clears the screen to remove what's typed from the screen buffer" );
   Console.Error.WriteLine( );
   Console.Error.WriteLine( "Written by Rob van der Woude" );
   return 1;
  }
 }
}

希望本文所述对大家的C#程序设计有所帮助。

相关文章

  • c# SendMail发送邮件实例代码

    c# SendMail发送邮件实例代码

    这篇文章介绍了c# SendMail发送邮件实例代码,有需要的朋友可以参考一下
    2013-09-09
  • C#中Equals和GetHashCode使用及区别

    C#中Equals和GetHashCode使用及区别

    这篇文章主要介绍了C#中Equals和GetHashCode使用及区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • C# ListView 点击表头对数据进行排序功能的实现代码

    C# ListView 点击表头对数据进行排序功能的实现代码

    这篇文章主要介绍了C# ListView 点击表头对数据进行排序功能的实现代码,需要的朋友可以参考下
    2017-04-04
  • C# 观察者模式实例介绍

    C# 观察者模式实例介绍

    一个简单的例子,比如说猫叫,老鼠跑,主人被惊醒,在不知道观察者模式之前,我们的代码可能是这样的
    2012-10-10
  • 详解三种C#实现数组反转方式

    详解三种C#实现数组反转方式

    本篇文章主要介绍了详解三种C#实现数组反转方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • C#中使用CliWrap让命令行交互举重若轻

    C#中使用CliWrap让命令行交互举重若轻

    这篇文章介绍了C#中使用CliWrap让命令行交互举重若轻,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • C#中Dictionary类使用实例

    C#中Dictionary类使用实例

    这篇文章主要介绍了C#中Dictionary类使用实例,本文直接给出一个使用实例,包含一些Dictionary的基本用法,需要的朋友可以参考下
    2015-06-06
  • C# 特性AttributeUsage简介与使用教程

    C# 特性AttributeUsage简介与使用教程

    这篇文章主要介绍了C# 特性AttributeUsage简介与使用教程,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • C#使用RabbitMQ的详细教程

    C#使用RabbitMQ的详细教程

    RabbitMQ 是一个功能强大的消息队列系统,可用于在分布式系统中进行可靠的消息传递,本篇博客将详细介绍如何在 C# 中使用 RabbitMQ 实现各种消息队列操作,并通过代码介绍的非常详细,需要的朋友可以参考下
    2024-08-08
  • 详解C#数据类型及其转换

    详解C#数据类型及其转换

    这篇文章主要介绍了C#数据类型及其转换详解,在C#中,数据类型可以分为几种类型,今天小编通过本文给大家详细介绍,需要的朋友可以参考下
    2020-07-07

最新评论