2011-09-21 40 views
3

我正在編寫一個程序,可以動態地創建小窗口窗體應用程序。 現在,exe文件生成正確。但是當我打開它時,顯示2個窗口。使用WindowsForm和CSharpCodeProvider的問題

http://i.stack.imgur.com/pAFlg.png

所以,我應該怎麼做才能禁用黑色窗口?

這裏是我的代碼:

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."; 
} 

此爲演示類構建:

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()); 
    } 
} 

謝謝你們。

回答

4

您應該更新您的編譯選項是這樣的:

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

要指示編譯器編譯一個Windows/GUI應用程序,而不是一個控制檯應用程序(這是默認值)。

+0

謝謝你,這是正確的答案。 – sindney

0

真的很酷的想法,但不是IronRuby或IronPython更好的方法來處理這種情況或什麼關於CLR中的Emit命名空間?

只是我的仙...