Runtime Compilation Example


using System;
using System.IO;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace CompilationTest {



    class Comp {

        public static int Main(string[] args) {

            
            CodeDomProvider cdp = new CSharpCodeProvider();

            bool result = Comp.CompileCode(cdp, "myfile.txt", "myexe.exe");

            Console.WriteLine("\tProgram will exit on its own in 7 seconds...");
            System.Threading.Thread.Sleep(7000);

            return 1;

        }


        public static bool CompileCode(CodeDomProvider provider, string sourceFile, string exeFile) {

            string PersonalFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            CompilerParameters cp = new CompilerParameters();
            CompilerResults cr = null;

            // Generate an executable instead of
            // a class library.
            cp.GenerateExecutable = true;

            // Set the assembly file name to generate.
            cp.OutputAssembly = exeFile;

            // Generate debug information.
            cp.IncludeDebugInformation = true;

            cp.ReferencedAssemblies.Add("mscorlib.dll");

            // Save the assembly as a physical file.
            cp.GenerateInMemory = false;

            // Set the level at which the compiler
            // should start displaying warnings.
            cp.WarningLevel = 3;

            // Set whether to treat all warnings as errors.
            cp.TreatWarningsAsErrors = false;

            // Set compiler argument to optimize output.
            cp.CompilerOptions = "/optimize";

            // Set a temporary files collection.
            // The TempFileCollection stores the temporary files
            // generated during a build in the current directory,
            // and does not delete them after compilation.
            cp.TempFiles = new TempFileCollection(PersonalFolder, true);

            Console.WriteLine();

            if (provider.Supports(GeneratorSupport.EntryPointMethod)) {
                // Specify the class that contains
                // the main method of the executable.
                //cp.MainClass = "Comp";


                // Invoke compilation.
                cr = provider.CompileAssemblyFromFile(cp, sourceFile);

                if (cr.Errors.Count > 0) {
                    // Display compilation errors.
                    Console.WriteLine("\tErrors building {0} into {1}",
                        sourceFile, cr.PathToAssembly);
                    foreach (CompilerError ce in cr.Errors) {
                        Console.WriteLine("  {0}", ce.ToString());
                        Console.WriteLine();
                    }
                } else {
                    Console.WriteLine("\tSource {0} built into {1} successfully.",
                        sourceFile, cr.PathToAssembly);
                    Console.WriteLine("\t{0} temporary files created during the compilation.",
                        cp.TempFiles.Count.ToString());
                }
            }//end if

            // Return the results of compilation.
            if (cr.Errors.Count > 0) {

                return false;

            } else {

                return true;
            }


        }//end method CompileCode()

    }//end class Comp

}//end namespace CompilationTest