【问题标题】:Can't use System.Windows.Forms无法使用 System.Windows.Forms
【发布时间】:2012-03-27 15:34:56
【问题描述】:

我尝试过(我的第一个)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? 如果是这样,应该是什么程序?我也尝试过使用表单,但我只显示一个窗口,没有代码。

【问题讨论】:

  • 有时错误消息实际上会告诉您出了什么问题:您缺少程序集引用。 How to: Add or Remove References in Visual Studio
  • 因为这只是您的第一个程序,最好重新启动并创建 Windows 窗体应用程序。 VS2010 项目向导将处理许多有关使用 windows 窗体的数据
  • dtb 评论的第一部分有点多余。显然,提问者缺少程序集引用。问题是如何解决这个问题,因为错误消息没有说明这一点。

标签: c# winforms visual-studio


【解决方案1】:

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

在解决方案资源管理器中右键单击您的项目并选择添加引用...,然后找到 System.Windows.Forms 并添加它。

【讨论】:

  • 好的,我在现有项目中遇到了这个问题。我认为项目文件被冲洗掉了,所以我从头开始创建、覆盖并添加了所有现有的类等文件。现在,当我转到添加引用时,浏览对话框的“.Net”选项卡中没有列出“System.Windows”和“System.Windows.Forms”。有什么想法吗?
  • @JayImerman 其在“程序集”选项卡下.. 抱歉,为时已晚。
  • 答案对我也很有用(+1),但是否可以在任何 c# 程序中永远添加 system.windows.forms 一次?使用您的答案,我们需要在任何新程序中始终这样做.....
  • 是否可以使用“dotnet”cli 添加引用?例如,“dotnet add package System.Windows.Forms”不起作用:error: There are no versions available for the package 'System.Windows.Forms'
【解决方案2】:

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

右键单击“引用”▶“添加新引用”▶“System.Windows.Forms”

【讨论】:

  • 右键单击“参考”▶“添加新参考”▶Assemblies▶“System.Windows.Forms”
【解决方案3】:

添加 System.Windows.Forms 引用需要 .NET Framework 项目类型:

我使用的是 .NET Core 项目类型。此项目类型不允许我们将程序集添加到其项目引用中。在将System.Windows.Forms 程序集添加到我的引用之前,我必须转移到 .NET Framework 项目类型,如Kendall Frey 答案中所述。

注意:COM 选项卡下有参考 System_Windows_Forms(适用于 .NET Core 和 .NET Framework)。这不是正确的。它必须是“程序集”选项卡下的System.Windows.Forms

【讨论】:

    【解决方案4】:

    要添加对“System.Windows.Forms”的引用,对于 Visual Studio Community 2017 来说似乎有点不同。

    1) 转到解决方案资源管理器并选择引用

    2) 右键单击​​并选择添加引用

    3) 在程序集中,检查 System.Windows.Forms 并按确定

    4) 就是这样。

    【讨论】:

      【解决方案5】:

      对于那些使用带有 .Net Core 6.0 的 Visual Studio 2022 的用户

      很抱歉恢复这个帖子,但我为此创建了一个帐户,因为我发现在谷歌搜索几天的解决方案都没有单独为我工作,而且似乎只显示过时的教程。

      Not Working =(

      什么对我有用

      1. 双击您的项目(打开 csproj 编辑器窗口

      添加以下行(替换现有的 TargetFramework 行):

      <TargetFramework>net6.0-windows</TargetFramework>
      <UseWindowsForms>true</UseWindowsForms>
      

      我的窗口,供参考,如下所示:

      <Project Sdk="Microsoft.NET.Sdk">
      
        <PropertyGroup>
          <OutputType>Exe</OutputType>
          <TargetFramework>net6.0-windows</TargetFramework>
            <UseWindowsForms>true</UseWindowsForms>
          <ImplicitUsings>enable</ImplicitUsings>
          <Nullable>enable</Nullable>
        </PropertyGroup>
      
      </Project>
      
      1. 注意这两行!我阅读了几个解决方案/视频,这些解决方案/视频在添加“UseWindowsForms”标签后就停止了,这对我来说并没有解决问题,即使在卸载和重新加载/关闭和打开等之后也是如此。李>

      我在应用“UseWindowsForms”标签后偶然发现了这个解决方案,然后在绝望中将我的 NET 框架的属性更改为 5.0,这导致它工作,然后在改回 6.0 时注意到它仍然工作。

      如果你以后忘记了Framework标签,你可以通过来回翻转你的属性来重现效果......

      1. 右键单击项目,然后转到属性。

      2. Change the Target framework from .NET 6.0...转....NET 5.0

      3. 退出回到您的代码。稍后将显示System.Windows.Forms connecting properly.

      4. 打开备份属性。

      5. 将 Target 框架从 .Net 5.0 更改回 .Net 6.0

      恭喜

      You have (or at least I have) a .Net 6.0 project that is properly allowing me to use System.Windows.Forms (Including the Clipboard, which I suspect many here are looking for...).

      我对此进行了尽职调查,在多个项目中对其进行了多次测试,并且它(至少对于我的设置)始终有效!

      顺便说一句。对于那些想知道的人。需要在图片中看到的 [STAThread] 属性标签才能让剪贴板类发挥作用。 (这也是我在示例中不使用顶级语句的原因,但如果您不需要该类,则该示例适用于顶级语句(我需要它来显示我的剪贴板测试......)

      【讨论】:

      • 非常感谢。我使用 VS 2022 和 .NET 6 花了一些时间来解决同样的问题。添加 'true' 解决了这个问题。再次感谢。
      • 谢谢老兄,让这个与 .NET 5 的 WPF 项目一起使用 vs 2019
      • 整个帖子可以简化为添加两个标签:net6.0-windows true
      【解决方案6】:

      确保解决方案资源管理器可见 在 MS Studio 2008 中转到查看并单击解决方案资源管理器

      在解决方案资源管理器中转到参考 右键单击参考并选择添加参考.. 选择 .NET 选项卡 向下滚动直到找到 System.Drawing -> 选择它 -> 点击 OK 按钮 对 System.Windows.Forms 执行相同操作

      当您运行表单时,这将起作用

      (埃迪生活在某个时间的某个地方)

      【讨论】:

        【解决方案7】:

        转到侧面项目面板,右键单击引用 -> 添加引用并找到 System.Windows.Forms

        任何时候出现这样的错误(您添加的某些命名空间显然已经丢失),解决方案可能就是这样 - 添加引用。

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

        【讨论】:

          【解决方案8】:

          可能是必要的, 取消引用 system.windows.forms 并再次参考。

          【讨论】:

            【解决方案9】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-08-17
              • 1970-01-01
              • 1970-01-01
              • 2010-09-20
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多