简体   繁体   English

如何在 c# 中以编程方式编译 ac# winform 应用程序

[英]How can i compile a c# winform application programmatically in c#

我创建了一个打开 ac# Winform 应用程序的工具,问题是在我对文件进行了一些修改之后,我想使用 c# 以编程方式重新编译这些文件。

You can do it by using CSharpCodeProvider .您可以使用CSharpCodeProvider来做到这一点。 You can find more information about this class in documentation .您可以在文档中找到有关class的更多信息。

after searching i have use this solution : String[] csFiles = CopyAllSourceFilesToCLASSESFolder().ToArray();搜索后我使用了这个解决方案: 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM