문제

I am trying to execute a bat file through my WPF application on a button click. I want the output of the batch file to be displayed in a TextBlock(with vertical scroll) of WPF application.

I am able to execute a bat file using Process.Start

Here is my Code

Process process = new Process();
process.StartInfo.FileName = @"C:\bin\run.bat";
process.StartInfo.Arguments = @"-X";
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
process.WaitForExit();

How to proceed further? Thanks

도움이 되었습니까?

해결책

I guess I will flesh out my comment with an answer. You need to redirect the output of your bat file, you do that by using Process.RedirectStandardOutput. Taking your code and the MSDN Library page's code will give you something like this.

Process process = new Process();
process.StartInfo.FileName = @"C:\bin\run.bat";
process.StartInfo.Arguments = @"-X";
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.StartInfo.UseShellExecute = false; //Changed Line
process.StartInfo.RedirectStandardOutput = true;  //Changed Line
process.Start();
string output = process.StandardOutput.ReadToEnd(); //Changed Line
process.WaitForExit(); //Moved Line

다른 팁

you need to redirect standard output to your text file using the gtr symbol ">". e.g

command>myfile.extension

and the opposite executes the commands in a file one line at a time e.g

command

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top