我正在编写一个可以动态创建小型Windows窗体应用程序的程序.现在,exe文件已正确生成.但是当我打开它时,显示2个窗口.那么我应该怎么做才能禁用该黑色窗口?这是我的代码:string sourceName = MyForm.cs; FileI...
![](https://oss.womengda.net/imgfile/2310/1ER2M0324B0-1c93.jpg)
我正在编写一个可以动态创建小型Windows窗体应用程序的程序.
现在,exe文件已正确生成.但是当我打开它时,显示2个窗口.
那么我应该怎么做才能禁用该黑色窗口?
这是我的代码:
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());
}
}
多谢你们.
解决方法:
您应该像这样更新编译器选项:
cp.CompilerOptions = "/target:winexe /optimize /win32icon:" + config.GetIconPath() + " MyForm.cs";
指示编译器编译Windows / GUI应用程序,而不是控制台应用程序(这是默认设置).
沃梦达教程
本文标题为:首页> C#>使用WindowsForm和CSharpCodeProvider问题
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
猜你喜欢
- nginx – AspNetCore Azure AD Connect回调URL是http,而不是https 2023-09-26
- c# – 我为什么要使用SqlCommand.CommandType = StoredProcedure? 2023-11-14
- C# Winfrom实现Skyline画直线功能的示例代码 2023-02-06
- c#中LINQ的基本用法(一) 2023-05-31
- C#如何使用Bogus创建模拟数据示例代码 2023-01-22
- C#可变参数params示例详解 2023-05-17
- C#比较日期的方法总结 2023-01-16
- C# pictureBox用法案例详解 2023-04-28
- C# Datatable的几种用法小结 2023-03-29
- C#高效比较两个DataTable数据差异化的方法实现 2023-06-08