I have a program where i have a .exe file. The work of the .exe is to scan the directories for corrupted files. If accessed through the command prompt in the following format i get the result of scan
"location of exe" "files or folders to be scanned"
the result i get is as the scan goes on. Like
D:\A scanned
D:\B scanned
D:\C scanned
D:\D scanned
Now my question is how can i get the result line by line using c#.
I'm using the following set of codes by i get only the end result. I need the output line by line
The code is as follows:
string tempGETCMD = null;
Process CMDprocess = new Process();
System.Diagnostics.ProcessStartInfo StartInfo = new System.Diagnostics.ProcessStartInfo();
StartInfo.FileName = "cmd"; //starts cmd window
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.CreateNoWindow = true;
StartInfo.RedirectStandardInput = true;
StartInfo.RedirectStandardOutput = true;
StartInfo.UseShellExecute = false; //required to redirect
CMDprocess.StartInfo = StartInfo;
CMDprocess.Start();
System.IO.StreamReader SR = CMDprocess.StandardOutput;
System.IO.StreamWriter SW = CMDprocess.StandardInput;
SW.WriteLine("@echo on");
SW.WriteLine(@"E:\Scanner\Scanner.exe -r E:\Files to be scanned\"); //the command you wish to run.....
SW.WriteLine("exit"); //exits command prompt window
tempGETCMD = SR.ReadToEnd(); //returns results of the command window
SW.Close();
SR.Close();
return tempGETCMD;
Any help in this would be apprecitated
Thanks,
I think you have to use a dedicated thread to read every new line from the StandardOutput
stream, something like this:
//This will append each new line to your textBox1 (multiline)
private void AppendLine(string line)
{
if (textBox1.InvokeRequired){
if(textBox1.IsHandleCreated) textBox1.Invoke(new Action<string>(AppendLine), line);
}
else if(!textBox1.IsDisposed) textBox1.AppendText(line + "\r\n");
}
//place this code in your form constructor
Shown += (s, e) => {
new Thread((() =>
{
while (true){
string line = CMDProcess.StandardOutput.ReadLine();
AppendLine(line);
//System.Threading.Thread.Sleep(100); <--- try this to see it in action
}
})).Start();
};
//Now every time your CMD outputs something, the lines will be printed (in your textBox1) like as you see in the CMD console window.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.