欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-09 13:47:02
本文实例讲述了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#程序设计有所帮助。