Powered by GeSHi

Source code for hideinput.cs

(view source code of hideinput.cs as plain text)

  1. using System;
  2.  
  3. namespace RobvanderWoude
  4. {
  5. 	class HideInput
  6. 	{
  7. 		static int Main( string[] args )
  8. 		{
  9. 			try
  10. 			{
  11. 				bool clearscreen = false;
  12.  
  13. 				if ( args.Length > 1 )
  14. 				{
  15. 					return WriteError( "Too many command line arguments" );
  16. 				}
  17. 				if ( args.Length == 1 )
  18. 				{
  19. 					switch ( args[0].ToUpper( ) )
  20. 					{
  21. 						case "/C":
  22. 							clearscreen = true;
  23. 							break;
  24. 						case "/?":
  25. 							return WriteError( );
  26. 						default:
  27. 							return WriteError( "Invalid command line argument \"" + args[0] + "\"" );
  28. 					}
  29. 				}
  30.  
  31. 				// Set console foreground color to background color to hide what's being typed
  32. 				ConsoleColor color = Console.ForegroundColor;
  33. 				Console.ForegroundColor = Console.BackgroundColor;
  34.  
  35. 				// Read 1 line of input from the console
  36. 				string input = Console.ReadLine( );
  37.  
  38. 				// Restore the original console foreground color
  39. 				Console.ForegroundColor = color;
  40.  
  41. 				// Clear the screen id specified on the command line
  42. 				if ( clearscreen )
  43. 				{
  44. 					Console.Clear( );
  45. 				}
  46.  
  47. 				// Display the input - which should be redirected for this program to be of any use
  48. 				Console.WriteLine( input );
  49.  
  50. 				// Returncode 0 for success, or 1 if the input was empty or whitespace only
  51. 				if ( string.IsNullOrWhiteSpace( input ) )
  52. 				{
  53. 					return 1;
  54. 				}
  55. 				else
  56. 				{
  57. 					return 0;
  58. 				}
  59. 			}
  60. 			catch ( Exception e )
  61. 			{
  62. 				return WriteError( e.Message );
  63. 			}
  64. 		}
  65.  
  66. 		public static int WriteError( string errorMessage = "" )
  67. 		{
  68. 			/*
  69. 			HideInput,  Version 1.00
  70. 			Batch utility to read 1 line of input while hiding what's being typed, by
  71. 			temporarily setting the console foreground color equal to its background color
  72.  
  73. 			Usage:  FOR /F "tokens=*" %%A IN ('HIDEINPUT') DO SET password=%%A
  74. 			   or:  FOR /F "tokens=*" %%A IN ('HIDEINPUT /C') DO SET password=%%A
  75.  
  76. 			Where:  /C  clears the screen to remove what's typed from the screen buffer
  77.  
  78. 			Written by Rob van der Woude
  79. 			http://www.robvanderwoude.com
  80. 			*/
  81.  
  82. 			Console.ResetColor( );
  83. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  84. 			{
  85. 				Console.Error.WriteLine( );
  86. 				Console.ForegroundColor = ConsoleColor.Red;
  87. 				Console.Error.Write( "ERROR:  " );
  88. 				Console.ForegroundColor = ConsoleColor.White;
  89. 				Console.Error.WriteLine( errorMessage );
  90. 				Console.ResetColor( );
  91. 			}
  92. 			Console.Error.WriteLine( );
  93. 			Console.Error.WriteLine( "HideInput,  Version 1.10" );
  94. 			Console.Error.WriteLine( "Batch utility to read 1 line of input while hiding what's being typed, by" );
  95. 			Console.Error.WriteLine( "temporarily setting the console foreground color equal to its background color" );
  96. 			Console.Error.WriteLine( );
  97. 			Console.Error.Write( "Usage:  FOR /F \"tokens=*\" %%A IN ('" );
  98. 			Console.ForegroundColor = ConsoleColor.White;
  99. 			Console.Error.Write( "HIDEINPUT" );
  100. 			Console.ResetColor( );
  101. 			Console.Error.WriteLine( "') DO SET password=%%A" );
  102. 			Console.Error.Write( "   or:  FOR /F \"tokens=*\" %%A IN ('" );
  103. 			Console.ForegroundColor = ConsoleColor.White;
  104. 			Console.Error.Write( "HIDEINPUT /C" );
  105. 			Console.ResetColor( );
  106. 			Console.Error.WriteLine( "') DO SET password=%%A" );
  107. 			Console.Error.WriteLine( );
  108. 			Console.Error.Write( "Where:  " );
  109. 			Console.ForegroundColor = ConsoleColor.White;
  110. 			Console.Error.Write( "/C" );
  111. 			Console.ResetColor( );
  112. 			Console.Error.WriteLine( "  clears the screen to remove what's typed from the screen buffer" );
  113. 			Console.Error.WriteLine( );
  114. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  115. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  116. 			return 1;
  117. 		}
  118.  
  119. 	}
  120. }
  121.