从C#代码运行* .cmd文件的多行命令

时间:2014-12-09 06:44:36

标签: c# batch-file command-line cmd

我在E驱动器E:\ 1 \ 2 \ Abc.cmd中有一个文件,并在此文件中写入以下行

@:again
@..\xyz.exe  param1 param2
@goto again

xyz.exe路径为E:\ 1 \ xyz.exe

如果我双击abc.cmd然后它工作正常但它在运行表单C#代码时抛出异常。说“..\xyz.exe is not recognized as internal or external commad”

我写了以下代码

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = @"E:\1\2\Abc.cmd";
process.StartInfo = startInfo;
process.Start();

1 个答案:

答案 0 :(得分:2)

使用此代码

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = "E:\\1\\2\\Abc.cmd";
startInfo.WorkingDirectory = "E:\\1\\2\\";
process.StartInfo = startInfo;
process.Start();