Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for isredirected.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	class IsRedirected
  8. 	{
  9. 		static string progver = "1.02";
  10.  
  11.  
  12. 		static int Main( string[] args )
  13. 		{
  14. 			int checkinput = 0;
  15. 			int checkoutput = 0;
  16. 			int checkerror = 0;
  17. 			int rc = 0;
  18.  
  19. 			if ( Console.IsInputRedirected )
  20. 			{
  21. 				checkinput = 2;
  22. 			}
  23. 			if ( Console.IsOutputRedirected )
  24. 			{
  25. 				checkoutput = 4;
  26. 			}
  27. 			if ( Console.IsErrorRedirected )
  28. 			{
  29. 				checkerror = 8;
  30. 			}
  31.  
  32. 			switch ( args.Length )
  33. 			{
  34. 				case 0:
  35. 					rc = checkinput + checkoutput + checkerror;
  36. 					break;
  37. 				case 1:
  38. 					switch ( args[0].ToLower( ) )
  39. 					{
  40. 						case "/i":
  41. 						case "-i":
  42. 							rc = checkinput;
  43. 							break;
  44. 						case "/o":
  45. 						case "-o":
  46. 							rc = checkoutput;
  47. 							break;
  48. 						case "/e":
  49. 						case "-e":
  50. 							rc = checkerror;
  51. 							break;
  52. 						case "/v":
  53. 						case "-v":
  54. 							Console.WriteLine( );
  55. 							Console.WriteLine( "Standard Input  redirection detected: {0}", ( Console.IsInputRedirected ? "Yes    +2" : "No     +0" ) );
  56. 							Console.WriteLine( "Standard Output redirection detected: {0}", ( Console.IsOutputRedirected ? "Yes    +4" : "No     +0" ) );
  57. 							Console.WriteLine( "Standard Error  redirection detected: {0}", ( Console.IsErrorRedirected ? "Yes    +8" : "No     +0" ) );
  58. 							Console.WriteLine( );
  59. 							rc = checkinput + checkoutput + checkerror;
  60. 							Console.WriteLine( "                         Return Code:        {0,2}", rc );
  61. 							break;
  62. 						case "/?":
  63. 						case "-?":
  64. 						case "/h":
  65. 						case "-h":
  66. 						case "/help":
  67. 						case "-help":
  68. 						case "--help":
  69. 							return ShowHelp( );
  70. 						default:
  71. 							return ShowHelp( "Invalid command line argument" );
  72. 					}
  73. 					break;
  74. 				default:
  75. 					return ShowHelp( "Invalid command line arguments" );
  76. 			}
  77.  
  78.  
  79. 			// Pass through any redirected StdIn
  80. 			if ( Console.IsInputRedirected )
  81. 			{
  82. 				string input = Console.In.ReadToEnd( );
  83. 				Console.Write( input );
  84. 			}
  85.  
  86. 			return rc;
  87. 		}
  88.  
  89.  
  90. 		static int ShowHelp( params string[] errmsg )
  91. 		{
  92. 			#region Error Message
  93.  
  94. 			if ( errmsg.Length > 0 )
  95. 			{
  96. 				List<string> errargs = new List<string>( errmsg );
  97. 				errargs.RemoveAt( 0 );
  98. 				Console.Error.WriteLine( );
  99. 				Console.ForegroundColor = ConsoleColor.Red;
  100. 				Console.Error.Write( "ERROR:\t" );
  101. 				Console.ForegroundColor = ConsoleColor.White;
  102. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  103. 				Console.ResetColor( );
  104. 			}
  105.  
  106. 			#endregion Error Message
  107.  
  108.  
  109. 			#region Help Text
  110.  
  111. 			/*
  112. 			IsRedirected,  Version 1.02
  113. 			Test redirection of Standard Input, Standard Output and Standard Error
  114.  
  115. 			Usage:  ISREDIRECTED  [ /E | /I | /O | /V ]
  116.  
  117. 			Where:  /E   returns result for Standard Error  redirection only (default: all)
  118. 			        /I   returns result for Standard Input  redirection only
  119. 			        /O   returns result for Standard Output redirection only
  120. 			        /V   Verbose mode: show detected redirections on screen
  121.  
  122. 			Notes:  The test result is returned as "errorlevel", where no redirection = 0,
  123. 			        command line error = 1, Standard Input redirection = +2, Standard
  124. 			        Output redirection = +4, and Standard Error redirection = +8.
  125. 			        Redirected Standard Input is passed on unaltered to this program's
  126. 			        Standard Output.
  127. 			        This version of the program requires .NET Framework 4.5.
  128.  
  129. 			Written by Rob van der Woude
  130. 			http://www.robvanderwoude.com
  131. 			*/
  132.  
  133. 			Console.Error.WriteLine( );
  134.  
  135. 			Console.Error.WriteLine( "IsRedirected,  Version {0}", progver );
  136.  
  137. 			Console.Error.WriteLine( "Test redirection of Standard Input, Standard Output and Standard Error" );
  138.  
  139. 			Console.Error.WriteLine( );
  140.  
  141. 			Console.Error.Write( "Usage:  " );
  142. 			Console.ForegroundColor = ConsoleColor.White;
  143. 			Console.Error.WriteLine( "ISREDIRECTED  [ /E | /I | /O | /V ]" );
  144. 			Console.ResetColor( );
  145. 			Console.Error.WriteLine( );
  146.  
  147. 			Console.Error.Write( "Where:  " );
  148. 			Console.ForegroundColor = ConsoleColor.White;
  149. 			Console.Error.Write( "/E" );
  150. 			Console.ResetColor( );
  151. 			Console.Error.Write( "   returns result for Standard " );
  152. 			Console.ForegroundColor = ConsoleColor.White;
  153. 			Console.Error.Write( "E" );
  154. 			Console.ResetColor( );
  155. 			Console.Error.WriteLine( "rror  redirection only" );
  156.  
  157. 			Console.ForegroundColor = ConsoleColor.White;
  158. 			Console.Error.Write( "        /I" );
  159. 			Console.ResetColor( );
  160. 			Console.Error.Write( "   returns result for Standard " );
  161. 			Console.ForegroundColor = ConsoleColor.White;
  162. 			Console.Error.Write( "I" );
  163. 			Console.ResetColor( );
  164. 			Console.Error.WriteLine( "nput  redirection only (default: all)" );
  165.  
  166. 			Console.ForegroundColor = ConsoleColor.White;
  167. 			Console.Error.Write( "        /O" );
  168. 			Console.ResetColor( );
  169. 			Console.Error.Write( "   returns result for Standard " );
  170. 			Console.ForegroundColor = ConsoleColor.White;
  171. 			Console.Error.Write( "O" );
  172. 			Console.ResetColor( );
  173. 			Console.Error.WriteLine( "utput redirection only" );
  174.  
  175. 			Console.ForegroundColor = ConsoleColor.White;
  176. 			Console.Error.Write( "        /V   V" );
  177. 			Console.ResetColor( );
  178. 			Console.Error.WriteLine( "erbose mode: show detected redirections on screen" );
  179.  
  180. 			Console.Error.WriteLine( );
  181.  
  182. 			Console.Error.WriteLine( "Notes:  The test result is returned as \"errorlevel\", where no redirection = 0," );
  183.  
  184. 			Console.Error.WriteLine( "        command line error = 1, Standard Input redirection = +2, Standard" );
  185.  
  186. 			Console.Error.WriteLine( "        Output redirection = +4, and Standard Error redirection = +8." );
  187.  
  188. 			Console.Error.WriteLine( "        Redirected Standard Input is passed on unaltered to this program's" );
  189.  
  190. 			Console.Error.WriteLine( "        Standard Output." );
  191.  
  192. 			Console.Error.WriteLine( "        This version of the program requires .NET Framework 4.5." );
  193.  
  194. 			Console.Error.WriteLine( );
  195.  
  196. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  197.  
  198. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  199.  
  200. 			#endregion Help Text
  201.  
  202. 			return 1;
  203. 		}
  204. 	}
  205. }
  206.  

page last modified: 2024-04-16; loaded in 0.0082 seconds