-1

I am trying to do a Visual Studio kind of program, which allows you to type code into a RichTextBox. After pressing F5(Compile), it would compile the code. How would the user compile said code? I know how to use the ConsoleApplication Compiler, but not compiling Windows Forms :(

Could someone help? A code-example is preferable, but at this point, I'll accept ANYTHING!

My current code for Console Apps is this:

        CSharpCodeProvider codeProvider = new CSharpCodeProvider();
        ICodeCompiler icc = codeProvider.CreateCompiler();
        string Output = "MCCACOut.exe";
            Button ButtonObject = (Button)sender;

            richTextBox201.Text = "";
            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            //Make sure we generate an EXE, not a DLL
            parameters.GenerateExecutable = true;
            parameters.OutputAssembly = Output;
            CompilerResults results = icc.CompileAssemblyFromSource(parameters, richTextBox301.Text);

            if (results.Errors.Count > 0)
            {
                richTextBox201.ForeColor = Color.Red;
                foreach (CompilerError CompErr in results.Errors)
                {
                    richTextBox201.Text = richTextBox201.Text +
                                "Line number " + CompErr.Line +
                                ", Error Number: " + CompErr.ErrorNumber +
                                ", '" + CompErr.ErrorText + ";" +
                                Environment.NewLine + Environment.NewLine;
                }
            }
            else
            {
                //Successful Compile
                richTextBox201.ForeColor = Color.Blue;
                richTextBox201.Text = "Success!";
                //If we clicked run then launch our EXE
                Process.Start(Output);
            }

Could anyone convert this to compile WinForms instead of ConsoleApp? :)

9
  • Look up how to build c# apps from commandline.. then run that. Commented Oct 23, 2019 at 10:09
  • No results matching my description(or yours) came up.(Google, StackOverFlow, etc.) All they say is "From CommandPrompt/Notepad" but that is not what I want... I need it to compile straight from richtextbox and create output .exe, and it should compile as WindowsForms. Commented Oct 23, 2019 at 10:12
  • Do you have any example code, or know anyone who could help? I am struggling :( Commented Oct 23, 2019 at 10:13
  • First Link when Googling msbuild winforms: stackoverflow.com/questions/708029/… Commented Oct 23, 2019 at 10:14
  • It didn't explain exactly HOW to use it :..( it only explained WHAT to use... I appreciate the link though :) is there any way to compile a Windows Forms App like the code I just provided in the question, but obviously differently? Commented Oct 23, 2019 at 10:18

2 Answers 2

1

I think you have to save your file with .cs extension and invoke a process to compile it using c# compiler csc.exe.

After saving the file, you can compile the code using,

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe [options] filename.cs

Take a look at this for options

You can invoke a process to do this from your IDE.

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe filename.cs";
process.StartInfo = startInfo;
process.Start();

Hope this helps.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi, Nishan! I liked your code a whole lot, but I was wondering.. How would I then start the compiled .exe? I tested, and it compiles. But I can't find the output .exe ...
Is it not in the same folder that your .cs file is in?
I don't think it was generated :( It should be in the same folder as my current project(that's where I saved it) but it's not .. Any idea how to fix?
I have a MenuStrip Item that is displayed "Compile Winforms C#", and when it is pressed, no errors occur(with the code you gave me.) ...But, it doesn't start the newly compiled app, and it doesn't even generate it, from what I see in the folder :(
Try to provide options to specify output C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:exe /out:filename.exe filename.cs
|
1

Finally, after many, many times of struggling, I got it to work :) With the help of Nishan Chathuranga

        string compiledOutput = "Generated.exe";

        //COMPILATION WORK
        String[] referenceAssemblies = { "System.dll", "System.Drawing.dll", "System.Windows.Forms.dll" };

        CodeDomProvider _CodeCompiler = CodeDomProvider.CreateProvider("CSharp");
        System.CodeDom.Compiler.CompilerParameters _CompilerParameters =
         new System.CodeDom.Compiler.CompilerParameters(referenceAssemblies, "");

        _CompilerParameters.OutputAssembly = compiledOutput;
        _CompilerParameters.GenerateExecutable = true;
        _CompilerParameters.GenerateInMemory = false;
        _CompilerParameters.WarningLevel = 3;
        _CompilerParameters.TreatWarningsAsErrors = true;
        _CompilerParameters.CompilerOptions = "/optimize /target:winexe";//!! HERE IS THE SOLUTION !!

        string _Errors = null;
        try
        {
            // Invoke compilation
            CompilerResults _CompilerResults = null;
            _CompilerResults = _CodeCompiler.CompileAssemblyFromSource(_CompilerParameters, richTextBox1.Text);

            if (_CompilerResults.Errors.Count > 0)
            {
                // Return compilation errors
                _Errors = "";
                foreach (System.CodeDom.Compiler.CompilerError CompErr in _CompilerResults.Errors)
                {
                    _Errors += "Line number " + CompErr.Line +
                    ", Error Number: " + CompErr.ErrorNumber +
                    ", '" + CompErr.ErrorText + ";\r\n\r\n";
                }
            }
        }
        catch (Exception _Exception)
        {
            // Error occurred when trying to compile the code
            _Errors = _Exception.Message;
        }



        //AFTER WORK
        if (_Errors == null)
        {
            // lets run the program
            MessageBox.Show(compiledOutput + " Compiled !");
            System.Diagnostics.Process.Start(compiledOutput);
        }
        else
        {
            MessageBox.Show("Error occurred during compilation : \r\n" + _Errors);
        }
  • This works like a charm!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.