-1

How to I parse live cmd output and display in a textbox. Example

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

gives the below in 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                                                                      

I want to parse the above and display in the step ( loading , counting ....) in one textbox and the progress in % ,using numbers in () , in another textbox and number of pages in another.

I tried to modify the answer from this How to parse command line output from c#? for my needs but it couldn't succeed. Then I read somewhere I need to use BackgroundWorker and tried to modify and use code from http://www.codeproject.com/Articles/99143/BackgroundWorker-Class-Sample-for-Beginners but failed.

Community
  • 1
  • 1

1 Answers1

0

You can use pProcess.StandardOutput.ReadToEnd(); like this:

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;
}

Update: Class system.io.streamreader has also a method ReadLine(), you should try it out as pProcess.StandardOutput is of this type

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

online Thomas
  • 8,864
  • 6
  • 44
  • 85