簡體   English   中英

在C#掛起中使用Process調用cmd.exe

[英]Calling cmd.exe using Process in C# hanging

我正在嘗試從cmd.exe(測試“ DIR”命令)中獲取標准,並將其放入文本框中。 但是,無論何時啟動該程序,程序都會掛起(不按任何按鈕)。

private void cmd_test()
{
    Process pr = new Process()
    {
        StartInfo = {
            FileName = "cmd.exe",
            UseShellExecute = true,
            CreateNoWindow = true,
            RedirectStandardOutput = true,
            RedirectStandardInput = true,
            RedirectStandardError = true,
        }
    };

    pr.Start();
    pr.StandardInput.WriteLine("DIR");
    TextBox1.Text = pr.StandardOutput.ReadToEnd();
}

我還嘗試了StartInfo塊而不是WriteLine中的Arguments = "DIR"

我如何正確地將命令發送到cmd.exe,而不將其掛起?

干得好:

  using (var p = new Process())
  {
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.Arguments = "/C dir";
    p.Start();
    var output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    richTextBox1.Text = output;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM