如何在运行时编译中保存生成的exe

时间:2016-12-01 08:39:02

标签: c# .net

我正在尝试通过CSharpProvider将桌面生成的代码保存到桌面。我怎么能这样做?

CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
string Output = "Out.exe";
Button ButtonObject = (Button)sender;

textBox2.Text = "";
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
//Make sure we generate an EXE, not a DLL
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, textBox1.Text);

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

我不想只是保存它。

1 个答案:

答案 0 :(得分:-1)

我已经解决了。将out.exe更改为c:\ out.exe。