Get console output in my WinForms application (in particular, C # compiler output - CSC.EXE)

I decided to make something really simple -

WinForms application with a text box for C # code and a button. When the button is clicked, I save the contents of the text field in the temp.cs file, call csc.exe (Process.Start ()) with the file name as a parameter, so that it will be compiled. This assumes that I set the PATH variables and that’s it.

When csc.exe displays syntax errors and so on, how can I return it and show it in another text field in my application?

NOTE My goal is not only to simply "build the C # code from my application (depending on what is possible)" .. this is to get csc.exe output

+4
source share
3 answers

You need to redirect stdout and stderr.

It goes line by line:

process = new Process(); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; process.StartInfo.CreateNoWindow = true; process.StartInfo.FileName = command; process.StartInfo.Arguments = arguments; 

Then read from process.StandardInput . I can’t remember if all this is necessary.

+4
source

Use the csc.exe file [parameters]> output.file to transfer the console output of csc.exe to a text file and then read the text file into a text box.

+1
source

Use the ProcessStartInfo class to redirect console output to your stream, and then display the contents of the stream in your Windows control.

You can also take a look at the CodeDom namespace to create assemblies on the fly, but that depends on your goals and requirements.

0
source

Source: https://habr.com/ru/post/1341156/


All Articles