Compile an exe during runtime...!!!!

avirag 3 Tallied Votes 553 Views Share

Hi all well i am writing my first snippet here, hope it will be helpful for all........:)

I am Create a windows form application (if you use console you will need to replace all the "MessageBox.Show()"s with "Console.WriteLine()" and change the the "+"s to ","s), then add reference to System.CodeDom.Compiler and System.IO. Then just add a button (or whatever you are gonna use to make your exe compile) and add the code)

ddanbe commented: Great! +6
public static bool CompileExecutable(String sourceName)
        {
            //Source file that you are compliling 
            FileInfo sourceFile = new FileInfo(sourceName);

            //Create a C# code provider 
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

            //Create a bool variable for to to use after the complie proccess to see if there are any erros
            bool compileOk = false;

             //Make a name for the exe
             String exeName = String.Format(@"{0}\{1}.exe",
             System.Environment.CurrentDirectory, sourceFile.Name.Replace(".", "_"));

             //Creates a variable, cp, to set the complier parameters
             CompilerParameters cp = new CompilerParameters();

             //You can generate a dll or a exe file, in this case we'll make an exe so we set this to true
             cp.GenerateExecutable = true;

             //Set the name
             cp.OutputAssembly = exeName;

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

             //Set the compliere to not treat warranings as erros
             cp.TreatWarningsAsErrors = false;

             //Make it compile 
             CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceName);

             //if there are more then 0 erros...
             if (cr.Errors.Count > 0)
             {
                 //A message box shows the erros that occured 
                 MessageBox.Show("Errors building {0} into {1}" +
                     sourceName + cr.PathToAssembly);

                 //for each error that occured in the code make a separete message box
                 foreach (CompilerError ce in cr.Errors)
                 {
                     MessageBox.Show("  {0}" + ce.ToString());
                 }
             }

             //if there are no erros...
             else
             {
                 //a message box shows compliere results and a success message
                 MessageBox.Show("Source {0} built into {1} successfully." +
                     sourceName + cr.PathToAssembly);
             }

             //if there are erros...
             if (cr.Errors.Count > 0)
             {
                 //the bool variable that we made in the beggining is set to flase so the functions returns a false
                 compileOk = false;
             }

             //if there are no erros...
             else
             {
                 //we are returning a true (success)
                 compileOk = true;
             }

             //return the result
             return compileOk;
        }

        //this is the code for the button, but don't have to use a button to do it you can make it on load or w/e
        private void button1_Click_1(object sender, EventArgs e)
        {
            //create a stream writer and make a .cs file
            TextWriter writer = new StreamWriter(Application.StartupPath + "\\myExe.cs");

            //we'll use the WriteLine() function to write all of our code
            writer.WriteLine("using System;");
            writer.WriteLine("using System.Collections.Generic;");
            writer.WriteLine("using System.Text;");
            writer.WriteLine("namespace TestAppi");
            writer.WriteLine("{");
            writer.WriteLine("class Program");
            writer.WriteLine("{");
            writer.WriteLine("static void Main(string[] args)");
            writer.WriteLine("{");
            writer.WriteLine("Console.WriteLine(\"Hello World\");");
            writer.WriteLine("Console.ReadLine();");
            writer.WriteLine("} } }");

            //close and save the file 
            writer.Close();
            
            //create a string to the file
            string file = Application.StartupPath + "\\myExe.cs";

            //compile it
            CompileExecutable(file);
        }
Problem Solwer 0 Newbie Poster

you have to first saVE UR FILEWITH .CS .eg.(Prashant.cs)

csc filesavedname.cs
then enter filesavename

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.