4

I decided to do something really simple -

A WinForms application with a text box for C# code and a button. When the button is clicked, I save the content of the text box to a temp .cs file, invoke the csc.exe (Process.Start()) with the file name as parameter so that it is compiled. This is assuming I set the PATH variables and everything.

When the csc.exe outputs syntax errors and stuff, how can I get it back and show it in another text box in my app?

NOTE My aim is not to just "complie C# code from within my app (in whichever way possible)".. it is to get the output of csc.exe

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • possible duplicate of [Is it possible to dynamically compile and execute C# code fragments?](http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-code-fragments) – Hans Passant Feb 24 '11 at 15:46
  • Well.. the other answer uses something called CodeDom. My question is not about "A way to compile C#". My question is about specifically using the csc.exe and get the output somehow. If I had asked "how can I compile C# from my program", it would be duplicate. –  Feb 24 '11 at 15:58
  • Edited question to make it clear: I need output of csc.exe Not just any way to compile C# code –  Feb 24 '11 at 16:01
  • Actually this is a duplicate of **[How to spawn a process and capture its STDOUT in .NET?](http://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net)** – Timwi Feb 24 '11 at 16:13
  • Hmmm.. Yep. Please close it! I was obsessed with CSC.exe I failed to think about it in a generic way. –  Feb 24 '11 at 16:16

3 Answers3

4

You need to redirect stdout and stderr.

It goes along the lines of:

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 reading from process.StandardInput. I can't really remember if this is all you need.

Vitor Py
  • 5,145
  • 4
  • 39
  • 62
1

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

DaveRead
  • 3,371
  • 1
  • 21
  • 24
0

Use a ProcessStartInfo class to redirect the console output to a stream of yours, then display the stream content in your windows control.

You can also take a look a the CodeDom namespace to produce on the fly assemblies, but it depends on your goal and requirement.

Steve B
  • 36,818
  • 21
  • 101
  • 174