我尝试从代码创建cmd命令,命令为:
Tools.IRSGenBuild\\Build.bat bah
这应该使用Build.bat
从bah文件创建一个文件我没有使用cmd命令,但是向我展示如何使用该命令的人使用命令设置文件夹:
cd c:\\Users\\Sam\\Desktop\\FinalProject\\IRSGen
我在代码中所做的是:
System.Diagnostics.Process processStart = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfoStart = new System.Diagnostics.ProcessStartInfo();
startInfoStart.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfoStart.FileName = "cmd.exe";
startInfoStart.Arguments = "/C cd c:\\Users\\Sam\\Desktop\\FinalProject\\IRSGen";
processStart.StartInfo = startInfoStart;
processStart.Start();
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C Tools.IRSGenBuild\\Build.bat bah";
process.StartInfo = startInfo;
process.Start();
Process.Start(@"C:\Users\Sam\Desktop\FinalProject\IRSGen\Tools.IRSGen\Products\Development\Assemblies");
最后一行是打开.bat产品的文件夹。
我得到了例外:
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
修改
这是我的代码:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = @"cmd.exe";
startInfo.WorkingDirectory = @"C:\Users\Sam\Desktop\FinalProject\IRSGen";
startInfo.Arguments = @"Tools.IRSGen\Build\Build.bat bah";
process.StartInfo = startInfo;
process.Start();
Process.Start(@"C:\Users\Sam\Desktop\FinalProject\IRSGen\Tools.IRSGen\Products");
它没有工作,当我从cmd运行它工作正常。我究竟做错了什么? (抛出异常是因为我试图打开未创建的文件夹,问题出在cmd命令中)