开发者

Problems using WindowsForm and CSharpCodeProvider

开发者 https://www.devze.com 2023-04-06 03:59 出处:网络
I\'m writing a program that can create small windows form application dynamically. Now, the exe file were generated correctly.But when i open it, there shows 2 windows.

I'm writing a program that can create small windows form application dynamically. Now, the exe file were generated correctly.But when i open it, there shows 2 windows.

Problems using WindowsForm and CSharpCodeProvider

So what should i do to disable that black window?

Here's my code:

string sourceName = "MyForm.cs";  
FileInfo sourceFile = new FileInfo(sourceName);  
CSharpCodeProvider provider = new CSharpCodeProvider();  

status_content_label.Text = "Exporting ... ";  

String exeName = String.Format(@"{0}\{1}.exe", "Output/", sourceFile.Name.Replace(".", "_"));  

CompilerParameters cp = new CompilerParameters();  

cp.ReferencedAssemblies.Add("System.dll");  
cp.ReferencedAssemblies.Add("System.Drawing.dll");  
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");  
//cp.ReferencedAssemblies.Add("AxInterop.ShockwaveFlashObjects.dll");  
//cp.ReferencedAssemblies.Add("Interop.ShockwaveFlashObjects.dll");  
//cp.ReferencedAssemblies.Add("System.Data.dll");  

// Generate an executable instead of   
// a class library.  
cp.GenerateExecutable = true;  

// Specify the assembly file name to generate.  
cp.OutputAssembly = exeName;  

// Save the assembly as a physical file.  
cp.GenerateInMemory = false;  

cp.IncludeDebugInformation = false;  

// Set whether to treat all warnings as errors.  
cp.TreatWarningsAsErrors = false;  

cp.CompilerOptions = "/optimize /win32icon:" + config.GetIconPath() + " MyForm.cs";  

// Invoke compilation of the source file.  
CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceName);  

string errorMessage;  

if (cr.Errors.Count > 0)  
{  
    // Display compilation errors.  
    errorMessage = "Errors building {0} into {1}" + sourceName + cr.PathToAssembly + "\n";  
    foreach (CompilerError ce in cr.Errors)  
    {  
        errorMessage += "  {0}" + ce.ToString() + "\n";  
    }  
    errorReport.ShowError(errorMessage);  
    errorReport.Show();  

    status_content_label.Text = "Failed to create the exe file.";  
}  
else
{  
    status_content_label.Text = "Exe file successfully created.";  
}

This is the demo class to build:

using System;  
using System.Drawing;  
using System.Windows.Forms;  

class MyForm : Form  
{  
    public MyForm()  
    {  
        this.Text = "Hello Worl开发者_开发知识库d";  
        this.StartPosition = FormStartPosition.CenterScreen;  
    }  

    public static void Main()  
    {  
        Application.Run(new MyForm());  
    }  
}

Thanks guys.


You should update your compiler options like this:

cp.CompilerOptions = "/target:winexe /optimize /win32icon:" + config.GetIconPath() + " MyForm.cs";  

To instruct the compiler to compile a Windows/GUI app, and not a Console app (which is the default).


Really cool idea, but isn't IronRuby or IronPython an better approach for this situation or whats about the Emit Namespace in CLR?

Just my cents...

0

精彩评论

暂无评论...
验证码 换一张
取 消