C# 在cmd中运行良好,在C中运行异常#
我有以下代码:C# 在cmd中运行良好,在C中运行异常#,c#,visual-studio,command-line,cmd,command,C#,Visual Studio,Command Line,Cmd,Command,我有以下代码: 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.Comp
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'的未处理异常。”
其他信息:系统找不到指定的文件
我复制了文本框1.Text,因为它在cmd中,工作正常。文件名和参数必须拆分并相应输入 只接受文件名,参数应该由另一个参数传递
System.Diagnostics.Process.Start(@"C:\Windows\System32\cmd.exe", "/c " + command);
文件名和参数必须拆分并相应输入 只接受文件名,参数应该由另一个参数传递
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.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
}