Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems using WindowsForm and CSharpCodeProvider

Tags:

c#

.net

winforms

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.

https://i.sstatic.net/pAFlg.png

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 World";  
        this.StartPosition = FormStartPosition.CenterScreen;  
    }  

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

Thanks guys.

like image 789
sindney Avatar asked Feb 16 '26 08:02

sindney


1 Answers

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).

like image 162
Simon Mourier Avatar answered Feb 17 '26 23:02

Simon Mourier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!