Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 名称应用程序在当前上下文中不存在_C# - Fatal编程技术网

C# 名称应用程序在当前上下文中不存在

C# 名称应用程序在当前上下文中不存在,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

我有一个现有的winform应用程序,它没有使用Program.cs。我想用通常的代码添加Program.cs

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