Get String from Cmd.exe C#
Lets say i got a .bat file, where the text is like echo HELLO pause
That outputs "HELLO". nothing else
If i start the process from C#, maybe i want to idle on the process and put everything it outputs to a string? Like i scan the process.
I got code, that does this, but after like 0 seconds, when process is started, it takes what it outputs, but only the first. If i got a bat file with 100 lines then it only takes 1 maybe.
Alltogether, As soon when a bat file outputs anything new, i want to get it in my C# app.
Please help
Sample code:
Directory.SetCurrentDirectory(@"C:\Users\WS\Desktop\Modded\");
Proces开发者_Go百科s p = new Process();
p.StartInfo.FileName = @"C:\Users\WS\Desktop\Modded\Launcher.bat";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
txtLog.Text =
p.StandardOutput.ReadToEnd();
p.Close();
~~ redpois0n
If I understand what you are asking correctly, it should return a string array and it sounds like you are only retrieving/displaying the first element of the array. If the command line has many arguments you need to just loop through the array from 0 until the size of the array.
Also the txtLog may need to be changed to: txtLog.Text += p.StandardOutput.ReadToEnd(); if you are appending to the log.
精彩评论