在C#中调用EXE文件(传参数、等待、返回结果)
2013-07-14 22:41
357 查看
在C#中调用EXE文件(传参数、等待、返回结果)
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();
转自: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# 调用带参数EXE文件及带启动参数EXE制作
- C#调用存储过程 返回值,输出参数,返回结果集
- C# 调用带参数EXE文件及带启动参数EXE
- C++使用system带参数调用exe程序并把结果输出到文件
- 使用ShellExecuteEx调用控制台程序或命令行,传入多个参数,并等待返回结果
- .NET调用控制台下生成的exe文件,传参及获取返回参数的思路及代码
- C# 调用exe 执行文件时附带参数
- C# 调用带参数EXE文件及带启动参数EXE制作
- .NET调用控制台下生成的exe文件,传参及获取返回参数
- C#调用带参数EXE文件及带启动参数EXE制作(转)
- java调用C的exe文件并传入参数,读出exe输出结果
- .NET调用控制台下生成的exe文件,传参及获取返回参数的思路及代码
- C# 调用带参数EXE文件及带启动参数EXE制作
- c# 调用外包程序 等待处理完成结果
- C#调用命令行返回参数方法
- 如何用C#程序调用bat文件的同时向bat文件传递类似字符串这样的参数?
- C#系列之在C#中调用EXE文件
- flash 调用启动一个.exe文件并向其传递参数的方法
- C#中调用存储过程(带返回参数 And 无返回参数)