【问题标题】:How do I parse live cmd output and display in a textbox如何解析实时 cmd 输出并在文本框中显示
【发布时间】:2016-02-04 22:45:55
【问题描述】:

如何解析实时 cmd 输出并在文本框中显示。示例

wkhtmltopdf http://www.google.com output.pdf 

在 cmd 中给出以下内容

Loading pages (1/6)
[>                                                           ] 0%
[======>                                                     ] 10%
[=============>                                              ] 23%
[==================>                                         ] 30%
[==============================>                             ] 51%
[=================================>                          ] 56%
[==================================>                         ] 58%
[========================================>                   ] 68%
[===============================================>            ] 79%
[================================================>           ] 81%
[==================================================>         ] 84%
[============================================================] 100%
Counting pages (2/6)                                               
[============================================================] Object 1 of 1
Resolving links (4/6)                                                       
[============================================================] Object 1 of 1
Loading headers and footers (5/6)                                           
Printing pages (6/6)
[>                                                           ] Preparing
[============================================================] Page 1 of 1
Done                                                                      

我想解析上面的内容并在一个文本框中的步骤(加载、计数 ....)中显示进度,在 () 中使用数字,在另一个文本框中使用数字,在另一个文本框中显示页数。

我尝试根据我的需要修改来自 How to parse command line output from c#? 的答案,但它无法成功。然后我在某个需要使用 BackgroundWorker 的地方阅读并尝试修改和使用来自http://www.codeproject.com/Articles/99143/BackgroundWorker-Class-Sample-for-Beginners 的代码但失败了。

【问题讨论】:

标签: c# visual-studio


【解决方案1】:

你可以像这样使用pProcess.StandardOutput.ReadToEnd();

public void GetMacAddress(string ipAddress)
{
    string macAddress = string.Empty;
    System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
    pProcess.StartInfo.FileName = "arp";
    pProcess.StartInfo.Arguments = "-a " + ipAddress;
    pProcess.StartInfo.UseShellExecute = false;
    pProcess.StartInfo.RedirectStandardOutput = true;
    pProcess.StartInfo.CreateNoWindow = true;
    pProcess.Start();
    string strOutput = pProcess.StandardOutput.ReadToEnd();
    Textbox.text = strOutput;
}

更新: system.io.streamreader 类也有 ReadLine() 方法,你可以试试,因为 pProcess.StandardOutput 就是这种类型

https://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx

【讨论】:

  • 这会等到最后。我想在它发生时显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 2019-02-07
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
相关资源
最近更新 更多