[C#] C#重定向标准输入到标准输出 →→→→→进入此内容的聊天室

来自 , 2019-04-03, 写在 C#, 查看 112 次.
URL http://www.code666.cn/view/f5bf0ba0
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace RobvanderWoude
  6. {
  7.         class Tee
  8.         {
  9.                 static int Main( string[] args )
  10.                 {
  11.  
  12.                         #region Command Line Parsing
  13.  
  14.                         string filename = String.Empty;
  15.  
  16.                         if ( args.Length == 1 )
  17.                         {
  18.                                 if ( args[0].IndexOf( "?" ) > -1 )
  19.                                 {
  20.                                         return WriteError( );
  21.                                 }
  22.                                 else
  23.                                 {
  24.                                         filename = args[0];
  25.                                 }
  26.                         }
  27.                         else
  28.                         {
  29.                                 if ( args.Length > 1 )
  30.                                 {
  31.                                         return WriteError( "Too many command line arguments" );
  32.                                 }
  33.                                 else
  34.                                 {
  35.                                         return WriteError( );
  36.                                 }
  37.                         }
  38.  
  39.                         #endregion
  40.  
  41.                         try
  42.                         {
  43.                                 if ( ConsoleEx.InputRedirected )
  44.                                 {
  45.                                         int inputn;
  46.                                         string inputc;
  47.                                         StreamWriter file = new StreamWriter( filename, true );
  48.                                         do
  49.                                         {
  50.                                                 inputn = Console.In.Read( );
  51.                                                 if ( inputn != -1 )
  52.                                                 {
  53.                                                         inputc = Convert.ToChar( Convert.ToByte( inputn ) ).ToString( );
  54.                                                         Console.Write( inputc );
  55.                                                         file.Write( inputc );
  56.                                                 }
  57.                                         } while ( inputn != -1 );
  58.                                         file.Close( );
  59.                                 }
  60.                                 return 0;
  61.                         }
  62.                         catch ( Exception e )
  63.                         {
  64.                                 return WriteError( e.Message );
  65.                         }
  66.                 }
  67.  
  68.  
  69.                 #region Redirection Detection
  70.  
  71.                 // Code to detect redirection by Hans Passant on StackOverflow.com
  72.                 // http://stackoverflow.com/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected
  73.                 public static class ConsoleEx
  74.                 {
  75.                         public static bool OutputRedirected
  76.                         {
  77.                                 get
  78.                                 {
  79.                                         return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdout ) );
  80.                                 }
  81.                         }
  82.  
  83.                         public static bool InputRedirected
  84.                         {
  85.                                 get
  86.                                 {
  87.                                         return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdin ) );
  88.                                 }
  89.                         }
  90.                        
  91.                         public static bool ErrorRedirected
  92.                         {
  93.                                 get
  94.                                 {
  95.                                         return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stderr ) );
  96.                                 }
  97.                         }
  98.  
  99.                         // P/Invoke:
  100.                         private enum FileType { Unknown, Disk, Char, Pipe };
  101.                         private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 };
  102.  
  103.                         [DllImport( "kernel32.dll" )]
  104.                         private static extern FileType GetFileType( IntPtr hdl );
  105.  
  106.                         [DllImport( "kernel32.dll" )]
  107.                         private static extern IntPtr GetStdHandle( StdHandle std );
  108.                 }
  109.  
  110.                 #endregion
  111.  
  112.                 #region Error Handling
  113.  
  114.                 public static int WriteError( Exception e = null )
  115.                 {
  116.                         return WriteError( e == null ? null : e.Message );
  117.                 }
  118.  
  119.                 public static int WriteError( string errorMessage )
  120.                 {
  121.                         Console.OpenStandardError( );
  122.                         if ( string.IsNullOrEmpty( errorMessage ) == false )
  123.                         {
  124.                                 Console.WriteLine( );
  125.                                 Console.ForegroundColor = ConsoleColor.Red;
  126.                                 Console.Write( "ERROR: " );
  127.                                 Console.ForegroundColor = ConsoleColor.White;
  128.                                 Console.WriteLine( errorMessage );
  129.                                 Console.ResetColor( );
  130.                         }
  131.  
  132.                         Console.WriteLine( );
  133.                         Console.WriteLine( "Tee,  Version 1.02" );
  134.                         Console.WriteLine( "Redirect Standard Input to Standard Output AND to a file" );
  135.                         Console.WriteLine( );
  136.                         Console.Write( "Usage:  " );
  137.                         Console.ForegroundColor = ConsoleColor.White;
  138.                         Console.WriteLine( "TEE  filename" );
  139.                         Console.ResetColor( );
  140.                         Console.WriteLine( );
  141.                         Console.WriteLine( "Where:  \"filename\" is the file the Standard Input will be appended to" );
  142.                         Console.WriteLine( );
  143.                         Console.Write( "Check for redirection by Hans Passant on " );
  144.                         Console.ForegroundColor = ConsoleColor.DarkGray;
  145.                         Console.WriteLine( "StackOverflow.com" );
  146.                         Console.WriteLine( "/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected" );
  147.                         Console.ResetColor( );
  148.                         Console.WriteLine( );
  149.                         Console.WriteLine( "Written by Rob van der Woude" );
  150.                         Console.WriteLine( "http://www.robvanderwoude.com" );
  151.                         Console.OpenStandardOutput( );
  152.                         return 1;
  153.                 }
  154.  
  155.                 #endregion
  156.         }
  157. }
  158. //csharp/7353

回复 "C#重定向标准输入到标准输出"

这儿你可以回复上面这条便签

captcha