Powered by GeSHi

Source code for isredirected.cs

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

  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. namespace RobvanderWoude
  5. {
  6. 	class IsRedirected
  7. 	{
  8. 		static int Main( string[] args )
  9. 		{
  10. 			try
  11. 			{
  12. 				bool chkInput = false;
  13. 				bool chkOutput = false;
  14. 				bool chkError = false;
  15.  
  16. 				// Check command line arguments (/I, /O and/or /E switches allowed)
  17. 				switch ( args.Length )
  18. 				{
  19. 					case 0:
  20. 						chkInput = ConsoleEx.InputRedirected;
  21. 						chkOutput = ConsoleEx.OutputRedirected;
  22. 						chkError = ConsoleEx.ErrorRedirected;
  23. 						break;
  24. 					case 1:
  25. 						switch ( args[0].ToLower( ) )
  26. 						{
  27. 							case "/i":
  28. 							case "-i":
  29. 								chkInput = ConsoleEx.InputRedirected;
  30. 								break;
  31. 							case "/o":
  32. 							case "-o":
  33. 								chkOutput = ConsoleEx.OutputRedirected;
  34. 								break;
  35. 							case "/e":
  36. 							case "-e":
  37. 								chkError = ConsoleEx.ErrorRedirected;
  38. 								break;
  39. 							case "/?":
  40. 							case "-?":
  41. 							case "/h":
  42. 							case "-h":
  43. 							case "/help":
  44. 							case "-help":
  45. 								// Display help text without error message
  46. 								return WriteError( string.Empty );
  47. 							default:
  48. 								return WriteError( "Invalid command line argument" );
  49. 						}
  50. 						break;
  51. 					default:
  52. 						// Display help text with error message
  53. 						return WriteError( "Invalid command line arguments" );
  54. 				}
  55.  
  56. 				int result = 0;
  57. 				if ( chkInput )
  58. 				{
  59. 					result += 2;
  60. 				}
  61. 				if ( chkOutput )
  62. 				{
  63. 					result += 4;
  64. 				}
  65. 				if ( chkError )
  66. 				{
  67. 					result += 8;
  68. 				}
  69.  
  70. 				// Pass through any redirected StdIn
  71. 				if ( ConsoleEx.InputRedirected )
  72. 				{
  73. 					string input = Console.In.ReadToEnd( );
  74. 					Console.Write( input );
  75. 				}
  76.  
  77. 				// Return code 0..14 (even)
  78. 				return result;
  79. 			}
  80. 			catch ( Exception e )
  81. 			{
  82. 				// Display help text with error message
  83. 				return WriteError( e );
  84. 			}
  85. 		}
  86.  
  87. 		// Code to detect redirection by Hans Passant on StackOverflow.com
  88. 		// http://stackoverflow.com/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected
  89. 		public static class ConsoleEx
  90. 		{
  91. 			public static bool OutputRedirected
  92. 			{
  93. 				get
  94. 				{
  95. 					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdout ) );
  96. 				}
  97. 			}
  98. 			public static bool InputRedirected
  99. 			{
  100. 				get
  101. 				{
  102. 					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdin ) );
  103. 				}
  104. 			}
  105. 			public static bool ErrorRedirected
  106. 			{
  107. 				get
  108. 				{
  109. 					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stderr ) );
  110. 				}
  111. 			}
  112.  
  113. 			// P/Invoke:
  114. 			private enum FileType { Unknown, Disk, Char, Pipe };
  115. 			private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 };
  116.  
  117. 			[DllImport( "kernel32.dll" )]
  118. 			private static extern FileType GetFileType( IntPtr hdl );
  119.  
  120. 			[DllImport( "kernel32.dll" )]
  121. 			private static extern IntPtr GetStdHandle( StdHandle std );
  122. 		}
  123.  
  124. 		// Code to display help and optional error message, by Bas van der Woude
  125. 		public static int WriteError( Exception e )
  126. 		{
  127. 			return WriteError( e == null ? null : e.Message );
  128. 		}
  129.  
  130. 		public static int WriteError( string errorMessage )
  131. 		{
  132. 			string fullpath = Environment.GetCommandLineArgs( ).GetValue( 0 ).ToString( );
  133. 			string[] program = fullpath.Split( '\\' );
  134. 			string exeName = program[program.GetUpperBound( 0 )];
  135. 			exeName = exeName.Substring( 0, exeName.IndexOf( '.' ) );
  136.  
  137. 			Console.OpenStandardError( );
  138. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  139. 			{
  140. 				Console.WriteLine( );
  141. 				Console.ForegroundColor = ConsoleColor.Red;
  142. 				Console.Write( "ERROR: " );
  143. 				Console.ForegroundColor = ConsoleColor.White;
  144. 				Console.WriteLine( errorMessage );
  145. 				Console.ResetColor( );
  146. 			}
  147. 			/*
  148. 			IsRedirected,  Version 1.01
  149. 			Detect redirection of StdIn, StdOut and/or StdErr
  150. 			(Standard Input, Standard Output and Standard Error respectively)
  151.  
  152. 			Usage:  ISREDIRECTED  [ /I | /O | /E ]
  153.  
  154. 			Where:  /I   returns result for StdIn  redirection only (default: all)
  155. 					/O   returns result for StdOut redirection only
  156. 					/E   returns result for StdErr redirection only
  157.  
  158. 			Note:   The result is returned as 'errorlevel', where
  159. 					no redirection = 0, command line error = 1
  160. 					redirected StdIn = +2, StdOut = +4, StdErr = +8
  161.  
  162. 			Check for redirection by Hans Passant on StackOverflow.com
  163. 			/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected
  164.  
  165. 			Written by Rob van der Woude
  166. 			http://www.robvanderwoude.com
  167. 			*/
  168. 			Console.WriteLine( );
  169. 			Console.WriteLine( "IsRedirected,  Version 1.01" );
  170. 			Console.WriteLine( "Detect redirection of StdIn, StdOut and/or StdErr" );
  171. 			Console.WriteLine( "(Standard Input, Standard Output and Standard Error respectively)" );
  172. 			Console.WriteLine( );
  173. 			Console.Write( "Usage:  " );
  174. 			Console.ForegroundColor = ConsoleColor.White;
  175. 			Console.WriteLine( "{0}  [ /I | /O | /E ]", exeName.ToUpper( ) );
  176. 			Console.ResetColor( );
  177. 			Console.WriteLine( );
  178. 			Console.Write( "Where:  " );
  179. 			Console.ForegroundColor = ConsoleColor.White;
  180. 			Console.Write( "/I" );
  181. 			Console.ResetColor( );
  182. 			Console.Write( "   returns result for Std" );
  183. 			Console.ForegroundColor = ConsoleColor.White;
  184. 			Console.Write( "I" );
  185. 			Console.ResetColor( );
  186. 			Console.WriteLine( "n  redirection only (default: all)" );
  187. 			Console.ForegroundColor = ConsoleColor.White;
  188. 			Console.Write( "        /O" );
  189. 			Console.ResetColor( );
  190. 			Console.Write( "   returns result for Std" );
  191. 			Console.ForegroundColor = ConsoleColor.White;
  192. 			Console.Write( "O" );
  193. 			Console.ResetColor( );
  194. 			Console.WriteLine( "ut redirection only" );
  195. 			Console.ForegroundColor = ConsoleColor.White;
  196. 			Console.Write( "        /E" );
  197. 			Console.ResetColor( );
  198. 			Console.Write( "   returns result for Std" );
  199. 			Console.ForegroundColor = ConsoleColor.White;
  200. 			Console.Write( "E" );
  201. 			Console.ResetColor( );
  202. 			Console.WriteLine( "rr redirection only" );
  203. 			Console.WriteLine( );
  204. 			Console.WriteLine( "Note:   The result is returned as \"errorlevel\", where" );
  205. 			Console.WriteLine( "        no redirection = 0, command line error = 1" );
  206. 			Console.WriteLine( "        redirected StdIn = +2, StdOut = +4, StdErr = +8" );
  207. 			Console.WriteLine( );
  208. 			Console.Write( "Check for redirection by Hans Passant on " );
  209. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  210. 			Console.WriteLine( "StackOverflow.com" );
  211. 			Console.WriteLine( "/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected" );
  212. 			Console.ResetColor( );
  213. 			Console.WriteLine( );
  214. 			Console.WriteLine( "Written by Rob van der Woude" );
  215. 			Console.WriteLine( "http://www.robvanderwoude.com" );
  216. 			Console.OpenStandardOutput( );
  217. 			return 1;
  218. 		}
  219. 	}
  220. }
  221.