سؤال

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