在 cmd 中运行良好,在 C# 中运行异常
Runs fine In cmd, Exception in C#
我有以下代码:
public void Run(string command) {
System.Diagnostics.Process.Start("C:\Windows\System32\cmd.exe /c " + command);
//textBox1.Text = "C:\Windows\System32\cmd.exe /c " + command;
}
在 visual studio 中它告诉我:
“System.dll 中发生了 'System.ComponentModel.Win32Exception' 类型的未处理异常
附加信息:系统找不到指定的文件
我复制了 cmd 中的 textBox1.Text 并且工作正常。
文件名和参数必须分开输入。
Process.Start Method (String, String) 只接受文件名,参数应由另一个参数传递。
System.Diagnostics.Process.Start(@"C:\Windows\System32\cmd.exe", "/c " + command);
所以我通过对运行流程进行流程描述找到了解决方案。我更喜欢较短的版本。所以我不会接受这个答案。
public void Run(string command) {
System.Diagnostics.ProcessStartInfo to_run = new System.Diagnostics.ProcessStartInfo();
to_run.FileName = "cmd";
to_run.Arguments = "/c "+ command;
to_run.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Hidden cmd
//Start a process based on to_run description
System.Diagnostics.Process executing = System.Diagnostics.Process.Start(to_run);
executing.WaitForExit(); //Don't go further until this function is finished
}
我有以下代码:
public void Run(string command) {
System.Diagnostics.Process.Start("C:\Windows\System32\cmd.exe /c " + command);
//textBox1.Text = "C:\Windows\System32\cmd.exe /c " + command;
}
在 visual studio 中它告诉我:
“System.dll 中发生了 'System.ComponentModel.Win32Exception' 类型的未处理异常 附加信息:系统找不到指定的文件
我复制了 cmd 中的 textBox1.Text 并且工作正常。
文件名和参数必须分开输入。
Process.Start Method (String, String) 只接受文件名,参数应由另一个参数传递。
System.Diagnostics.Process.Start(@"C:\Windows\System32\cmd.exe", "/c " + command);
所以我通过对运行流程进行流程描述找到了解决方案。我更喜欢较短的版本。所以我不会接受这个答案。
public void Run(string command) {
System.Diagnostics.ProcessStartInfo to_run = new System.Diagnostics.ProcessStartInfo();
to_run.FileName = "cmd";
to_run.Arguments = "/c "+ command;
to_run.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Hidden cmd
//Start a process based on to_run description
System.Diagnostics.Process executing = System.Diagnostics.Process.Start(to_run);
executing.WaitForExit(); //Don't go further until this function is finished
}