在C#中调用EXE文件

1. 如果exe文件的返回值是int类型,标识操作执行的结果是否成功,例如:

class Program

    {

        static int Main(string[] args)

        {

            return args.Length;

        }

 }

则在调用exe文件时,可以用如下方法:

Process myProcess = new Process();

string fileName = @"C:/Test.exe";

string para =@"你好 北京欢迎你!";

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(fileName, para);

myProcess.StartInfo = myProcessStartInfo;

myProcess.Start();

while (!myProcess.HasExited)

{

   myProcess.WaitForExit();

}

int returnValue = myProcess.ExitCode;

 

2. 如果exe文件是将输出内容写入标准流,例如:

class Program

    {

        static void Main(string[] args)

        {

            Console.Write(args[0] + args[1] + args [2]);           

        }

 }

则在调用exe文件时,可以用如下方法:

string fileName = @"C:/Test.exe";

Process p = new Process();

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.FileName = fileName;

p.StartInfo.CreateNoWindow = true;

p.StartInfo.Arguments = "你好, 北京 欢迎你!";//参数以空格分隔,如果某个参数为空,可以传入””

p.Start();

p.WaitForExit();

string output = p.StandardOutput.ReadToEnd();

原文地址:https://www.cnblogs.com/xiaoyusmile/p/2280911.html