有没有办法将我的控制台应用程序转换为 C# 中的 Windows 窗体应用程序?

发布于 2024-08-23 20:54:17 字数 411 浏览 2 评论 0原文

我创建了一个控制台应用程序,但我想将其转换为 Windows 窗体应用程序。

我发现这个< /a> 它似乎是我所需要的,但是当我尝试使用 using System.Windows.Forms; 时收到一条错误消息;

这是我收到的错误消息:

错误 1 ​​命名空间“System”中不存在类型或命名空间名称“Windows”(是否缺少程序集引用?)

是否还有其他步骤,或者在 VS 2008 中是否有某种不同?

I created a console application, but I want to turn it into a windows forms application.

I found This and it appeared to be what I needed, but I got an error message when I tried to use using System.Windows.Forms;

This is the error message I got:

Error 1 The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)

Is there another step, or is it somehow different in VS 2008?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

走过海棠暮 2024-08-30 20:54:17

您需要添加对 WinForms 程序集的引用

  • 右键单击​​解决方案并选择“添加引用”
  • 选择 System.Windows.Forms 并单击“确定”

您可能还需要对 System.Data 执行相同的操作,具体取决于您的项目设置

You need to add a reference to the WinForms assembly

  • Right click on the solution and select "Add Reference"
  • Select System.Windows.Forms and hit OK

You may need to do the same for System.Data as well depending on your project setup

病女 2024-08-30 20:54:17

确保在项目的引用中添加 System.Windows.Forms 程序集。在解决方案资源管理器中,右键单击“引用”,然后在 .NET 选项卡下找到 System.Windows.Forms 程序集并添加它。

Make sure you add the System.Windows.Forms assembly in your references for the project. In the solution explorer, right click on 'References' and then under the .NET tab find the System.Windows.Forms assembly and add it.

冷默言语 2024-08-30 20:54:17

您需要添加对 System.Windows.Forms 的引用。右键单击您的项目并选择添加引用。

在 .NET 选项卡上选择前面提到的参考。

You need to add a reference to System.Windows.Forms. Right-click your project and choose Add Reference.

On the .NET tab choose the the previously mentioned reference.

天煞孤星 2024-08-30 20:54:17

最简单的方法:

从 .Net Core 3 开始,最简单的方法是将您的 Program.cs 备份到磁盘上的某个位置(或仅使用 git)并运行以下命令位于 Program.cs 所在位置:

dotnet new winforms --force

它将替换您的 Program.cs.csproj。然后只需从旧的 Program.cs 中复制所需的代码即可。就是这样!

对于手动转换项目,这可能会有所帮助:

以下是 .Net Core 3 项目的外观(使用dotnet new winforms生成):

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

以下是Program. cs 寻找一个新项目:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LinkInterceptor
{
    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());
        }
    }
}

The easiest way:

Starting with .Net Core 3 the easiest way to do it is to backup your Program.cs somewhere on a disk (or just using git) and run the following command where Program.cs is located:

dotnet new winforms --force

It will replace your Program.cs and .csproj. Then just copy required code from your old Program.cs. That's it!

For manually converting the project this may be helpful:

Here is how .Net Core 3 project looks like (generated using dotnet new winforms):

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

Here is how Program.cs looks for a new project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LinkInterceptor
{
    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());
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文