C# 名称应用程序在当前上下文中不存在
我有一个现有的winform应用程序,它没有使用Program.cs。我想用通常的代码添加Program.csC# 名称应用程序在当前上下文中不存在,c#,C#,我有一个现有的winform应用程序,它没有使用Program.cs。我想用通常的代码添加Program.cs static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableV
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());
}
}
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
但我有错误信息
应用程序在当前上下文中不存在
如何解决此问题?您需要在文件开头为
System.Windows.Forms
使用指令:
using System.Windows.Forms;
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());
}
}
…尽管我建议使用第一种解决方案。该解决方案存在于System.Windows.Forms命名空间中。您需要在该文件的顶部添加一个引用,或者显式地对路径进行质量控制。确保您添加了对System.Windows.Forms
程序集的引用,并添加了对System.Windows.Forms
命名空间的引用。Program.cs文件是否包含using System.Windows.Forms代码>?应用程序
类位于该命名空间中。如果您使用VS,请使用Alt+Shift+F10,它将帮助您,或者安装
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.Run(new Form1());