I am working in c# 4.0, i want to generate an executable file dynamically, so i used Code Dome, but when i executes it open in console and after then my form displays, i want to generate winform executable file. How can i achieve my aim. the code is below :
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.";
}
Specify the output type to be a Windows application by using the
CompilerOptions
: