如何从代码运行命令行命令

cal*_*sto 3 .net c# command-line batch-file

我需要做两件事:运行批处理文件(工作正常),并运行命令(不起作用).该命令的方法抛出异常'file not found'.如果我打开一个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);
        }

    }
Run Code Online (Sandbox Code Playgroud)

注释的代码也会抛出相同的异常.

Ore*_*ost 9

DEVCON ReScan真的是可执行文件的名称吗?我猜可执行文件是DEVCON,而ReScan是一个参数.这意味着您必须将StartInfo.FileName设置为"DEVCON"并将StartInfo.Arguments设置为"ReScan".