1

I'm using CSharpCodeProvider class to compile c# code.

for example the code is,

using System;

namespace Foo
{
    public class Bar
    {
        static void Main(string[] args)
        {
            Bar.SayHello();
            Console.ReadLine();
        }

        public static void SayHello()
        {
            Console.WriteLine("Hello World");
        }
    }
}

What I want is to get the final string output after execution, in this case "Hello World". The object of CompilerResults which is returned has an output field but it online shows messages about compilation process. The output file with .exe extension is also created just fine and It shows that string.

I'm looking for either a way to extract that string from exe file generated, or any other way.

EDIT: Is there a way to get the output string WITHOUT running that process and extracting it from there?

Mudasir Soomro
  • 140
  • 1
  • 1
  • 9
  • This may or may not be the same question, have a look: http://stackoverflow.com/questions/12648290/interacting-with-assembly-generated-by-csharpcodeprovider – Anders Forsgren Apr 11 '13 at 10:48
  • Do you want to extract data from the generated program file, or do you want to *run* the program and get the output? – Guffa Apr 11 '13 at 10:49
  • 3
    You say you have the `.exe` generated correctly. Is "Run the exe using [Process.Start](http://msdn.microsoft.com/en-gb/library/system.diagnostics.process.start.aspx) and redirect standard output" a suitable answer? If so, I'll post the code :) – RB. Apr 11 '13 at 10:51
  • @RB. Yes all I need is the string. Anyway possible. – Mudasir Soomro Apr 11 '13 at 10:55
  • You could write the output to a file? – Jens Kloster Apr 11 '13 at 10:56
  • @Guffa I want the string output. That "Hello World", anyway. – Mudasir Soomro Apr 11 '13 at 10:56
  • @JensKloster How exactly do I get the output is my question, what I'll do with it is unrelated :) – Mudasir Soomro Apr 11 '13 at 10:57
  • 1
    @MudasirSoomro Check out http://www.codeproject.com/Articles/18577/How-to-redirect-Standard-Input-Output-of-an-applic for how to redirect the standard output (you shouldn't need to redirect standard error or standard input for your purposes) – RB. Apr 11 '13 at 10:57
  • 1
    @MudasirSoomro It is an unsual request - therefore I thinkt your context is relevant. Maybe there is a smarter way to achieve your goal :) – Jens Kloster Apr 11 '13 at 11:27
  • Do you even need to compile the code? Does the code differ much, or would it be possible to get the string from the source code? – Guffa Apr 11 '13 at 13:08
  • @Guffa You don't get the problem. See, using socket programming I'm making a server that connects to different clients. The clients can send in their code that requires them to output a string. After receiving the code, I compile the code, (And yes it WILL differ, in fact there will be test cases to test the string output) get the string and send acknowledgement after checking it. – Mudasir Soomro Apr 11 '13 at 14:39
  • @RB. Thank you. It works, although the program is stuck if the code contains "Console.ReadLine". I have to either close the console window explicitly in order to continue or I have to send StandardInput everytime Console.ReadLine occurs. – Mudasir Soomro Apr 11 '13 at 14:42
  • @MudasirSoomro: Of course I didn't get the problem, you hadn't explained it. :) – Guffa Apr 11 '13 at 18:00

0 Answers0