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

时间:2015-11-04 12:24:20

标签: c# visual-studio

如何解析实时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中的代码但是失败了。

1 个答案:

答案 0 :(得分:0)

您可以像这样使用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

相关问题