Monday, September 8, 2014

Compile C# Code

In today's post I want to show, how one can compile C# code with C# and that way create and execute new programs during runtime.
This is relatively easy, amonst others .Net offers the class CodeDomProvider, with which directly code can be compiled. More can be found on MSDN, from there I also took the example.
The following program compiles the C# code entered in a textbox, creates out of it an .exe file and eventually runs it. The code should be self explaining, I will first list it and after that explain the important parts. There are 2 textboxes, in the one the source code is entered, in the other the compile result (success or the found errors) is shown. Furthermore there are 2 buttons, one compiles the code, the other executes the created program:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
using System.CodeDom.Compiler;

namespace CodeCompiler
    public partial class Form1 : Form
        public Form1()

        public void CompileExecutable(String[] sourceName)
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

            CompilerParameters cp = new CompilerParameters();

            cp.GenerateExecutable = true;
            cp.OutputAssembly = "Compiled.exe";
            cp.GenerateInMemory = false;
            cp.TreatWarningsAsErrors = false;

            CompilerResults cr = provider.CompileAssemblyFromSource(cp, sourceName);

            if (cr.Errors.Count > 0)
                textBox2.Text = "Errors: " + Environment.NewLine;
                foreach (CompilerError ce in cr.Errors)
                    textBox2.Text += ce.ToString() + Environment.NewLine;
                textBox2.Text = "Success";

        private void button1_Click(object sender, EventArgs e)
            string[] SourceCode = new string[1];
            SourceCode[0] = textBox1.Text;

        private void button2_Click(object sender, EventArgs e)

In the first line of the function CompileExecutable() a new code provider for C# is created. Then 4 compiling options are defined: Via GenerateExecutable = true we tell the program, that we want to create an executable file and not, for example, a class library. Via the next lines we define, that we want to create a file and set the output path, furthermore the handling of warnings.
Via CompileAssemblyFromSource() we eventually compile the code. To this function we have to pass over the created compiling arguments and an array of strings, which contains the source codes - so multiple programs can be compiled at once.
Afterwards the number of errors is checked, and if this is greater than 0, these are outputted.
Via System.Diagnostics.Process.Start() we then simply start the program.

1 comment:

  1. Here, you can read great Beginner and Advance C# Programming tutorials...

    C# Programming Tutorials