【问题标题】:How do I run Command line commands from code如何从代码运行命令行命令
【发布时间】:2010-10-29 12:30:03
【问题描述】:

我需要做两件事:运行一个批处理文件(工作正常),并运行一个命令(不工作)。 该命令的方法引发异常“找不到文件”。如果我打开一个 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);
        }

    }

注释的代码也会抛出同样的异常。

【问题讨论】:

    标签: c# .net command-line batch-file


    【解决方案1】:

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

    【讨论】:

    • 出于某种有趣的原因,这是 System.Diagnostics.Process 和本机 ShellExecute(Ex) 的常见错误,尽管它显然是一个“FileName”参数:)
    • 我会将 Filename 设置为文件名 (devcon.exe) 并使用 proc.StartInfo.Arguments 传入 ReScan 参数。只是为了清楚地表明什么是什么。
    • 我在使用 Process 时会定期做同样的事情(这并不经常)...
    【解决方案2】:

    DEVCON 应用程序实际上是否在工作目录中? 否则,除非您指定它的完整路径,否则它将无法工作。

    此外,您必须指定扩展名,所以我想您会选择“Devcon.exe”, 并在参数中而不是在文件名中指定参数:)

    【讨论】:

    • Devcon 位于 PATH 环境目录中。它正在工作:proc.StartInfo.FileName = "DEVCON"; proc.StartInfo.Arguments = "重新扫描";
    【解决方案3】:

    试试这个:

            ProcessStartInfo psi = new ProcessStartInfo();            
            psi.FileName = Environment.GetEnvironmentVariable("comspec");
            psi.CreateNoWindow = true;
            psi.RedirectStandardError = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardOutput = true;
            psi.UseShellExecute = false;
    
            Process p = Process.Start(psi);
    
            ConsoleColor fc = Console.ForegroundColor;
    
            StreamWriter sw = p.StandardInput;
            StreamReader sr = p.StandardOutput;
    
            char[] buffer = new char[1024];
            int l = 0;
    
            sw.Write("DEVCON ReScan");
            sw.Write(sw.NewLine);
    
            Console.Write(">> ");
    
            l = sr.Read(buffer, 0, buffer.Length);
    
            for (int n = 0; n < l; n++)
                Console.Write(buffer[n] + " ");
    
            p.Close();
    

    【讨论】:

      猜你喜欢
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多