要应用到ProcessStartInfo类的参数,先创建一个测试TestApp2.exe, 测试代码如下:
namespace TestApp2
{
class Program
{
static void Main(string\[\] args)
{
if (args.Length >= 2)
{
int sum = 0;
for (int i = 0; i < args.Length; i++)
{
sum += Convert.ToInt32(args\[i\]);
}
Console.WriteLine(sum.ToString());
}
Console.ReadLine();
}
}
}
创建Process启动程序如下:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "TestApp2.exe";
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.Arguments = "1 2 3 4 ";
try
{
using (Process ps = Process.Start(startInfo))
{
ps.WaitForExit();
Console.WriteLine("Exit");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
启动的同时,传出参数 startInfo.Arguments = "1 2 3 4 ";,应该可以预测到输出应该是10,因为TestApp2中是将输入参数转换为整型数字相加的,并显示结果。
执行成功,并将参数传给了TestApp2执行。