using System; using System.Collections.Generic; namespace RobvanderWoude { class IsRedirected { static string progver = "1.02"; static int Main( string[] args ) { int checkinput = 0; int checkoutput = 0; int checkerror = 0; int rc = 0; if ( Console.IsInputRedirected ) { checkinput = 2; } if ( Console.IsOutputRedirected ) { checkoutput = 4; } if ( Console.IsErrorRedirected ) { checkerror = 8; } switch ( args.Length ) { case 0: rc = checkinput + checkoutput + checkerror; break; case 1: switch ( args[0].ToLower( ) ) { case "/i": case "-i": rc = checkinput; break; case "/o": case "-o": rc = checkoutput; break; case "/e": case "-e": rc = checkerror; break; case "/v": case "-v": Console.WriteLine( ); Console.WriteLine( "Standard Input redirection detected: {0}", ( Console.IsInputRedirected ? "Yes +2" : "No +0" ) ); Console.WriteLine( "Standard Output redirection detected: {0}", ( Console.IsOutputRedirected ? "Yes +4" : "No +0" ) ); Console.WriteLine( "Standard Error redirection detected: {0}", ( Console.IsErrorRedirected ? "Yes +8" : "No +0" ) ); Console.WriteLine( ); rc = checkinput + checkoutput + checkerror; Console.WriteLine( " Return Code: {0,2}", rc ); break; case "/?": case "-?": case "/h": case "-h": case "/help": case "-help": case "--help": return ShowHelp( ); default: return ShowHelp( "Invalid command line argument" ); } break; default: return ShowHelp( "Invalid command line arguments" ); } // Pass through any redirected StdIn if ( Console.IsInputRedirected ) { string input = Console.In.ReadToEnd( ); Console.Write( input ); } return rc; } static int ShowHelp( params string[] errmsg ) { #region Error Message if ( errmsg.Length > 0 ) { List errargs = new List( errmsg ); errargs.RemoveAt( 0 ); Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR:\t" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) ); Console.ResetColor( ); } #endregion Error Message #region Help Text /* IsRedirected, Version 1.02 Test redirection of Standard Input, Standard Output and Standard Error Usage: ISREDIRECTED [ /E | /I | /O | /V ] Where: /E returns result for Standard Error redirection only (default: all) /I returns result for Standard Input redirection only /O returns result for Standard Output redirection only /V Verbose mode: show detected redirections on screen Notes: The test result is returned as "errorlevel", where no redirection = 0, command line error = 1, Standard Input redirection = +2, Standard Output redirection = +4, and Standard Error redirection = +8. Redirected Standard Input is passed on unaltered to this program's Standard Output. This version of the program requires .NET Framework 4.5. Written by Rob van der Woude http://www.robvanderwoude.com */ Console.Error.WriteLine( ); Console.Error.WriteLine( "IsRedirected, Version {0}", progver ); Console.Error.WriteLine( "Test redirection of Standard Input, Standard Output and Standard Error" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "ISREDIRECTED [ /E | /I | /O | /V ]" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/E" ); Console.ResetColor( ); Console.Error.Write( " returns result for Standard " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "E" ); Console.ResetColor( ); Console.Error.WriteLine( "rror redirection only" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /I" ); Console.ResetColor( ); Console.Error.Write( " returns result for Standard " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "I" ); Console.ResetColor( ); Console.Error.WriteLine( "nput redirection only (default: all)" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /O" ); Console.ResetColor( ); Console.Error.Write( " returns result for Standard " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "O" ); Console.ResetColor( ); Console.Error.WriteLine( "utput redirection only" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /V V" ); Console.ResetColor( ); Console.Error.WriteLine( "erbose mode: show detected redirections on screen" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Notes: The test result is returned as \"errorlevel\", where no redirection = 0," ); Console.Error.WriteLine( " command line error = 1, Standard Input redirection = +2, Standard" ); Console.Error.WriteLine( " Output redirection = +4, and Standard Error redirection = +8." ); Console.Error.WriteLine( " Redirected Standard Input is passed on unaltered to this program's" ); Console.Error.WriteLine( " Standard Output." ); Console.Error.WriteLine( " This version of the program requires .NET Framework 4.5." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "http://www.robvanderwoude.com" ); #endregion Help Text return 1; } } }