我需要做两件事:运行一个批处理文件(工作正常),并运行一个命令(不工作)。 该命令的方法抛出异常“找不到文件”。如果我打开一个 cmd 窗口并键入命令,它会完美运行。
private static void Rescan()
{
//System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("DEVCON ReScan");
//psi.RedirectStandardOutput = true;
//psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//psi.UseShellExecute = false;
//System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "DEVCON ReScan";
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
System.IO.StreamReader myOutput = proc.StandardOutput;
proc.WaitForExit(4000);
if (proc.HasExited)
{
string output = myOutput.ReadToEnd();
FileIO.WriteLog(_writePath, output);
}
}
注释的代码也抛出同样的异常。
最佳答案
DEVCON ReScan
真的是可执行文件的名称吗?我猜可执行文件是 DEVCON,而 ReScan 是一个参数。这意味着您必须将 StartInfo.FileName 设置为“DEVCON”并将 StartInfo.Arguments 设置为“ReScan”。
关于c# - 如何从代码运行命令行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/919803/