如何防止在控制台中打开我的动态生成的exe

本文关键字:动态 exe 我的 何防止 控制台 | 更新日期: 2023-09-27 18:24:08

我在c#4.0中工作,我想动态生成一个可执行文件,所以我使用了Code Dome,但当我在控制台中打开它执行时,然后我的表单显示出来,我想生成winform可执行文件。我怎样才能达到我的目标。代码如下:

string Code = @"
using System;
using System.Windows.Forms;
namespace CSBSS
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public class Form1 : Form
{
}
}
";
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
string tempFolder = @"..'DynamicOutput";
string Output = System.IO.Path.Combine(tempFolder, @"CSBSS.exe");
if (!System.IO.Directory.Exists(tempFolder))
{
    System.IO.Directory.CreateDirectory(tempFolder);
}
else
{
    if (System.IO.File.Exists(Output)) System.IO.File.Delete(Output);
}
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
parameters.TempFiles = new TempFileCollection(tempFolder, false);
//Make sure we generate an exe.
parameters.GenerateExecutable = true;
parameters.GenerateInMemory = false;
parameters.OutputAssembly = Output;

CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, Code);
string OutputMsg = "";
if (results.Errors.Count > 0)
{
    string msgDescr = "";
    foreach (CompilerError CompErr in results.Errors)
    {
        msgDescr += "Line number " + CompErr.Line +
                    ", Error Number: " + CompErr.ErrorNumber +
                    ", '" + CompErr.ErrorText + ";" +
                    Environment.NewLine + Environment.NewLine;
    }
    OutputMsg = @"Error occured while generating executable file, please check following internal error 
" + msgDescr;
    //return false;
}
else
{
    OutputMsg = "Executable file has been generated successfully.";
}

如何防止在控制台中打开我的动态生成的exe

使用CompilerOptions:指定输出类型为Windows应用程序

parameters.CompilerOptions = "/target:winexe";