当前位置: 移动技术网 > IT编程>开发语言>c# > C#隐藏控制台键盘输入的方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#隐藏控制台键盘输入的方法。分享给大家供大家参考。具体如下: using system; namespace robvanderwoude {

本文实例讲述了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#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网