• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

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

OC/C/C++ 水墨上仙 2379次浏览

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 = "" )
		{
			/*
			HideInput,  Version 1.00
			Batch utility to read 1 line of input while hiding what's being typed, by
			temporarily setting the console foreground color equal to its background color
			Usage:  FOR /F "tokens=*" %%A IN ('HIDEINPUT') DO SET password=%%A
			   or:  FOR /F "tokens=*" %%A IN ('HIDEINPUT /C') DO SET password=%%A
			Where:  /C  clears the screen to remove what's typed from the screen buffer
			Written by Rob van der Woude
			http://www.robvanderwoude.com
			*/
			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" );
			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
			return 1;
		}
	}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#隐藏控制台的键盘输入
喜欢 (0)
加载中……