Home > Net >  How can i compile a c# winform application programmatically in c#
How can i compile a c# winform application programmatically in c#

Time:06-06

I have created a tool that open a c# Winform application, the question is after I made some modification to the files I want to recompile those files programmatically using c#.

CodePudding user response:

You can do it by using CSharpCodeProvider. You can find more information about this class in documentation.

CodePudding user response:

after searching i have use this solution : String[] csFiles = CopyAllSourceFilesToCLASSESFolder().ToArray();

        String[] referenceAssemblies = { "System.dll", "System.Drawing.dll", "System.Windows.Forms.dll", "ICSharpCode.TextEditor.dll", "System.Xml.dll", "System.IO.dll", "System.ComponentModel.dll" , "System.Data.dll" , "System.linq.dll" };


            CSharpCodeProvider codeProvider = new CSharpCodeProvider();
            ICodeCompiler icc = codeProvider.CreateCompiler();
            string Output = "Out.exe";

            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(referenceAssemblies);
            //Make sure we generate an EXE, not a DLL
            parameters.GenerateExecutable = true;
            parameters.OutputAssembly = Output;

            CompilerResults results = icc.CompileAssemblyFromFileBatch(parameters, csFiles);

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

            Process.Start(Output);
  • Related