不能使用System.Windows.Forms

我试过(我的第一个)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"); 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? 如果是的话,应该是什么程序? 我也尝试过一种forms,但我只是显示一个窗口,没有代码。

控制台应用程序不会自动添加对System.Windows.Forms.dll的引用。

在解决scheme资源pipe理器中右键单击您的项目,然后select添加引用…然后findSystem.Windows.Forms并添加它。

您必须将名称空间的引用:System.Windows.Forms添加到您的项目中,因为由于某种原因它尚未添加,所以您可以从Visual Studio菜单中添加新build引用。

右键单击“参考”▶“添加新的参考”▶“System.Windows.Forms”

只需添加对System.Windows.Forms.dll的引用

确保解决scheme资源pipe理器可见在MS Studio 2008中查看并单击解决scheme资源pipe理器

在解决scheme资源pipe理器去参考右键单击参考并select添加引用。select.NET选项卡向下滚动,直到您findSystem.Drawing – >select它 – >单击确定button做相同的System.Windows.Forms

当你运行你的表单,这将工作

(eddie住在某个地方)

转到侧面项目面板,右键点击引用 – >添加引用,findSystem.Windows.Forms

任何时候发生这样的错误(你添加的一些命名空间是显然存在的),解决scheme可能是这样的 – 添加一个引用。

这是需要的,因为你的默认项目不包括所有东西,因为你可能不需要它,所以它节省了空间。 一个好的做法是排除你不使用的东西。