您的位置:首页 > 编程语言 > C#

在C#中调用EXE文件(传参数、等待、返回结果)

2013-07-14 22:41 357 查看
在C#中调用EXE文件(传参数、等待、返回结果)


转自:http://www.cnblogs.com/xiaoyusmile/archive/2011/12/08/2280911.html

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C#