C# 调用外部exe程序

版权声明: https://blog.csdn.net/niuge8905/article/details/83111401

有时候dll不能引用,那就只能另外做一个exe程序,然后通过调用这个程序就可以解决问题,但往往需要在本地生成一个中间数据。虽然有name一点麻烦,但也挺好用。

这里就是一个调用外部程序的方法。

/// <summary>
		/// 通过进程调用外部程序
		/// </summary>
		/// <param name="exePath"></param>
		/// <param name="argument"></param>
		public static void RunExeByProcess(string exePath, string argument)
		{
			//开启新线程
			System.Diagnostics.Process process = new System.Diagnostics.Process();
			//调用的exe的名称
			process.StartInfo.FileName = exePath;
			//传递进exe的参数
			process.StartInfo.Arguments = argument;
			process.StartInfo.UseShellExecute = false;
			//不显示exe的界面
			process.StartInfo.CreateNoWindow = true;
			process.StartInfo.RedirectStandardOutput = true;
			process.StartInfo.RedirectStandardInput = true;
			process.Start();

			process.StandardInput.AutoFlush = true;
			process.WaitForExit();
		}

如果想获取调用程序返回的的结果,那么只需要把上面的稍加修改增加返回值即可:

public static string RunExeByProcess(string exePath, string argument)
		{
			//开启新线程
			System.Diagnostics.Process process = new System.Diagnostics.Process();
			//调用的exe的名称
			process.StartInfo.FileName = exePath;
			//传递进exe的参数
			process.StartInfo.Arguments = argument;
			process.StartInfo.UseShellExecute = false;
			//不显示exe的界面
			process.StartInfo.CreateNoWindow = true;
			process.StartInfo.RedirectStandardOutput = true;
			process.StartInfo.RedirectStandardInput = true;
			process.Start();

			process.StandardInput.AutoFlush = true;

			string result = null;
			while (!process.StandardOutput.EndOfStream)
			{
				result += process.StandardOutput.ReadLine() + Environment.NewLine;
			}
			process.WaitForExit();
			return result;
		}

猜你喜欢

转载自blog.csdn.net/niuge8905/article/details/83111401