using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.CodeDom.Compiler; using System.Reflection; using Microsoft.CSharp; namespace FUDBuilder { public partial class frmMain : Form { CSharpCodeProvider compiler = new CSharpCodeProvider(); public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { // some sample code that shows a message box string code = "using System; using System.Windows.Forms;\nnamespace TestCode { class TestClass { static void Main() { MessageBox.Show(\"Hello, runtime compiled world!\"); } } }"; Compile(code, "compiled.exe"); } private void Compile(string code, string fileName) { CompilerParameters cparams = new CompilerParameters(); cparams.GenerateExecutable = true; // we want to build an EXE cparams.GenerateInMemory = false; // don't generate this in memory cparams.OutputAssembly = fileName; // set the output file name cparams.ReferencedAssemblies.Add("System.dll"); cparams.ReferencedAssemblies.Add("System.Windows.Forms.dll"); cparams.CompilerOptions = "/optimize /target:winexe"; // optimise and set the target to a WinForms application (change to /target:exe if you want console) // compile CompilerResults result = compiler.CompileAssemblyFromSource(cparams, code); // if there are errors in the code, tell the user foreach (CompilerError error in result.Errors) { MessageBox.Show(string.Format("Line {0}: Error {1} - {2}", error.Line, error.ErrorNumber, error.ErrorText)); } } } }