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

本文关键字:命令 文件 cmd 代码 运行 | 更新日期: 2023-09-27 18:33:35

我在 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();

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

使用此代码

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();
相关文章: