C# 无法使用System.Windows.Forms
我已经尝试过制作我的第一个C程序:C# 无法使用System.Windows.Forms,c#,winforms,visual-studio,C#,Winforms,Visual Studio,我已经尝试过制作我的第一个C程序: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("hello");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");
Console.ReadLine();
}
}
}
这很好,但如果我尝试使用System.Windows.Forms:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");
System.MessageBox("hello");
Console.ReadLine();
}
}
}
这是我得到的错误:
Error 1 The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\Users\Ramy\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 5 14 ConsoleApplication1
一些细节:
-我正在使用Visual Studio 2012;
-我已经安装了.NET开发工具包;
-它是一个控制台应用程序
可能是因为在控制台上,应用程序不能使用System.Windows.Forms?
如果是,应该是什么计划?我也尝试过使用表单,但我只显示了一个窗口,没有显示代码。您必须将名称空间的引用:System.Windows.Forms添加到项目中,因为由于某些原因,它尚未添加,因此您可以从Visual Studio菜单添加新引用
右键单击引用▶ 添加新引用▶ System.Windows.Forms控制台应用程序不会自动添加对System.Windows.Forms.dll的引用
在解决方案资源管理器中右键单击项目,然后选择“添加引用…”。。。然后找到System.Windows.Forms并添加它。确保解决方案资源管理器可见 在MS Studio 2008中,转到查看并单击解决方案资源管理器 在解决方案资源管理器中,转到参考 右键单击“引用”,然后选择“添加引用”。。 选择.NET选项卡 向下滚动,直到找到系统。绘图->选择它->单击确定按钮 对System.Windows.Forms执行相同的操作 当您运行表单时,这将起作用
eddie生活在某个时间点只需添加对System.Windows.Forms.dll的引用转到侧面的项目面板,右键单击引用->添加引用并查找System.Windows.Forms 每当出现类似这样的错误时,您添加的某个名称空间就会丢失,很明显,解决方案可能是添加引用 这是必需的,因为您的默认项目不包含所有内容,因为您可能不需要它,这样可以节省空间。一个好的做法是排除你没有使用的东西。可能是必要的, 取消引用system.windows.forms
和再次引用。要添加对System.Windows.Forms的引用,Visual Studio Community 2017似乎有点不同 1转到解决方案资源管理器并选择引用 2右键单击并选择“添加引用” 3在程序集中,检查System.Windows.Forms并按ok
4就是这样。添加System.Windows.Forms引用需要.NET Framework项目类型: 我使用的是.NET核心项目类型。此项目类型不允许我们将程序集添加到其项目引用中。在将System.Windows.Forms程序集添加到我的引用之前,我必须转到.NET Framework项目类型,如答案中所述
注意:COM选项卡下提供了适用于.NET Core和.NET Framework的参考系统\u Windows\u表单。这不是正确的。必须是“程序集”选项卡下的System.Windows.Forms。有时错误消息实际上会告诉您出了什么问题:您缺少程序集引用。因为这只是您的第一个程序,所以最好重新启动并创建Windows窗体应用程序。VS2010项目向导将处理许多有关windows窗体使用的数据。dtb评论的第一部分有点不必要。显然,asker缺少一个程序集引用。问题是如何解决这个问题,因为错误消息没有阐明这一点。好的,我在一个现有项目中遇到了这个问题。我认为项目文件已经过时了,所以我从头开始创建、重写并添加了所有现有的类等文件。现在,当我转到添加引用时,浏览对话框的.Net选项卡中没有列出System.Windows和System.Windows.Forms。有什么想法吗?@JayImerman在“装配”选项卡下。。抱歉,太晚了几年。这个答案对我+1也很有用,但在任何c程序中是否可以永远添加system.windows.forms一次?使用您的答案,我们需要在任何新程序中始终执行此操作…右键单击参考▶ 添加新引用▶ 集会▶ System.Windows.Forms