⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 consoleapp.cs

📁 c#源代码
💻 CS
字号:
//
// ${App.Name}
//
// Copyright (C) 2004 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;

namespace ICSharpCode.NAntAddIn.Tests.ConsoleApp
{
	class ConsoleApp
	{
		/// <summary>
		/// Command line argument telling the app to echo the parameter text
		/// back to the caller.
		/// </summary>
		public static readonly string EchoArgument = "-echo";
		
		/// <summary>
		/// Command line argument telling the app to send the contents of
		/// the text file back to the caller via standard output.
		/// </summary>
		public static readonly string FileArgument = "-file";

		/// <summary>
		/// Command line argument telling the app to echo the parameter text
		/// back to the caller via standard error.
		/// </summary>
		public static readonly string ErrorEchoArgument = "-error.echo";
		
		/// <summary>
		/// Command line argument telling the app to send the contents of
		/// the text file back to the caller via standard error.
		/// </summary>
		public static readonly string ErrorFileArgument = "-error.file";

		/// <summary>
		/// Command line argument telling the app to return a 
		/// particular exit code.
		/// </summary>
		public static readonly string ExitCodeArgument = "-exitcode";
		
		/// <summary>
		/// Argument that will keep the console app running forever until
		/// it is killed.
		/// </summary>
		public static readonly string ForeverArgument = "-forever";
		
		public static int Main(string[] args)
		{
			ConsoleApp App = new ConsoleApp();
			return App.Run(args);
		}
		
		public int Run(string[] args)
		{
			int exitCode = 0;
			
			if (args.Length > 0) {
				string firstArg = args[0];
				
				if (firstArg.StartsWith(EchoArgument)) {
					Console.WriteLine(firstArg.Substring(EchoArgument.Length + 1));
				} else if(firstArg.StartsWith(FileArgument)) {
					string outputText = ReadTextFile(firstArg.Substring(FileArgument.Length + 1));
					Console.Write(outputText);
				} else if(firstArg.StartsWith(ErrorEchoArgument)) {
					Console.Error.WriteLine(firstArg.Substring(ErrorEchoArgument.Length + 1));				          	
				} else if(firstArg.StartsWith(ErrorFileArgument)) {
					string outputText = ReadTextFile(firstArg.Substring(ErrorFileArgument.Length + 1));
					Console.Error.Write(outputText);
				} else if(firstArg.StartsWith(ExitCodeArgument)) {
					exitCode = Convert.ToInt32(firstArg.Substring(ExitCodeArgument.Length + 1));
				} else if(firstArg == ForeverArgument) {
					Thread.Sleep(Timeout.Infinite);
				}
			}
			
			return exitCode;
		}
		
		private string ReadTextFile(string filename)
		{			
			FileStream stream = File.Open(filename, FileMode.Open);
			
			byte[] bytesRead = new byte[stream.Length];
			
			stream.Read(bytesRead, 0, bytesRead.Length);
			string readText = UnicodeEncoding.UTF8.GetString(bytesRead);
	
			stream.Close();
			
			return readText;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -