之间有什么区别:
var startInfo = new ProcessStartInfo();
string path = Directory.GetCurrentDirectory() + @"\foldername";
startInfo.WorkingDirectory = path;
startInfo.FileName = path + @"\do_run.cmd";
startInfo.Arguments = "/c arg1 arg2";
Process.Start(startInfo);
和
var startInfo = new ProcessStartInfo();
string path = Directory.GetCurrentDirectory() + @"\foldername";
startInfo.FileName = @"C:\windows\system32\cmd.exe";
startInfo.Arguments = @"/c cd " + path + " && do_run arg1 arg2";
Process.Start(startInfo);
出于某种原因,第二个代码块正常工作,但第一个块没有 .
其次,我在发布应用程序时无法使用 C:
目录,那么如何在Visual Studio项目文件夹中运行 cmd.exe
?
谢谢
2 回答
像这样的东西:
只需使用
cmd.exe
:默认情况下,Windows将在系统PATH变量中包含System32(其中
cmd.exe
所在的位置)(这意味着您可以从任何地方运行cmd.exe
并找到它) .至于为什么你的第一个代码不是100%肯定,但如果你在.NET Core上运行,你可能需要将
UseShellExecute
设置为true
,因为与.NET Framework不同,它默认为false
. 也就是说,我认为以上是更好的选择 .