• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#重定向标准输入到标准输出

OC/C/C++ 水墨上仙 2495次浏览

C#重定向标准输入到标准输出

using System;
using System.IO;
using System.Runtime.InteropServices;
namespace RobvanderWoude
{
	class Tee
	{
		static int Main( string[] args )
		{
			#region Command Line Parsing
			string filename = String.Empty;
			if ( args.Length == 1 )
			{
				if ( args[0].IndexOf( "?" ) > -1 )
				{
					return WriteError( );
				}
				else
				{
					filename = args[0];
				}
			}
			else
			{
				if ( args.Length > 1 )
				{
					return WriteError( "Too many command line arguments" );
				}
				else
				{
					return WriteError( );
				}
			}
			#endregion
			try
			{
				if ( ConsoleEx.InputRedirected )
				{
					int inputn;
					string inputc;
					StreamWriter file = new StreamWriter( filename, true );
					do
					{
						inputn = Console.In.Read( );
						if ( inputn != -1 )
						{
							inputc = Convert.ToChar( Convert.ToByte( inputn ) ).ToString( );
							Console.Write( inputc );
							file.Write( inputc );
						}
					} while ( inputn != -1 );
					file.Close( );
				}
				return 0;
			}
			catch ( Exception e )
			{
				return WriteError( e.Message );
			}
		}
		#region Redirection Detection
		// Code to detect redirection by Hans Passant on StackOverflow.com
		// http://stackoverflow.com/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected
		public static class ConsoleEx
		{
			public static bool OutputRedirected
			{
				get
				{
					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdout ) );
				}
			}
			public static bool InputRedirected
			{
				get
				{
					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdin ) );
				}
			}
			
			public static bool ErrorRedirected
			{
				get
				{
					return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stderr ) );
				}
			}
			// P/Invoke:
			private enum FileType { Unknown, Disk, Char, Pipe };
			private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 };
			[DllImport( "kernel32.dll" )]
			private static extern FileType GetFileType( IntPtr hdl );
			[DllImport( "kernel32.dll" )]
			private static extern IntPtr GetStdHandle( StdHandle std );
		}
		#endregion
		#region Error Handling
		public static int WriteError( Exception e = null )
		{
			return WriteError( e == null ? null : e.Message );
		}
		public static int WriteError( string errorMessage )
		{
			Console.OpenStandardError( );
			if ( string.IsNullOrEmpty( errorMessage ) == false )
			{
				Console.WriteLine( );
				Console.ForegroundColor = ConsoleColor.Red;
				Console.Write( "ERROR: " );
				Console.ForegroundColor = ConsoleColor.White;
				Console.WriteLine( errorMessage );
				Console.ResetColor( );
			}
			Console.WriteLine( );
			Console.WriteLine( "Tee,  Version 1.02" );
			Console.WriteLine( "Redirect Standard Input to Standard Output AND to a file" );
			Console.WriteLine( );
			Console.Write( "Usage:  " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.WriteLine( "TEE  filename" );
			Console.ResetColor( );
			Console.WriteLine( );
			Console.WriteLine( "Where:  \"filename\" is the file the Standard Input will be appended to" );
			Console.WriteLine( );
			Console.Write( "Check for redirection by Hans Passant on " );
			Console.ForegroundColor = ConsoleColor.DarkGray;
			Console.WriteLine( "StackOverflow.com" );
			Console.WriteLine( "/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected" );
			Console.ResetColor( );
			Console.WriteLine( );
			Console.WriteLine( "Written by Rob van der Woude" );
			Console.WriteLine( "http://www.robvanderwoude.com" );
			Console.OpenStandardOutput( );
			return 1;
		}
		#endregion
	}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#重定向标准输入到标准输出
喜欢 (0)
加载中……