将 Winform 应用程序转换为控制台应用程序

发布于 2024-09-13 05:34:16 字数 140 浏览 2 评论 0原文

是否有一种快速而肮脏的方法(或 VS 宏)将 WinForms 应用程序转换为控制台应用程序?我有许多此类应用程序不再需要作为 Winforms 应用程序。我想我可以创建一堆新项目并复制粘贴模块和类,但如果只是删除我拥有的单一表单并编辑/删除一些内容,我宁愿这样做。

Is there a quick and dirty way (or a VS Macro) to convert a WinForms app to a Console App? I have a number of these apps that are no longer needed as Winforms apps. I suppose I could create a bunch of new projects and copy paste modules and classes over, but if it's just a matter of removing the single form that I have and editing/deleting a few things, I'd rather do that.

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

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

发布评论

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

评论(5

许久 2024-09-20 05:34:16

您可以更改项目设置以创建控制台应用程序而不是 Windows 应用程序(只需在解决方案窗口中的项目上按 Ctrl+Enter,然后进行更改)。

这会将应用程序编译为控制台应用程序,然后您将获得一个控制台。但是,您仍然会收到表格。如果您不想使用 Windows 窗体,则需要删除窗体,并用命令行参数或配置替换其功能。然后,您可以删除 Windows 窗体引用,它将被转换。

You can change the project settings to create a Console Application instead of a Windows Application (just hit Ctrl+Enter on the project in the Solution Window, and make that change).

That will compile the application as a Console Application, and you'll get a console. However, you'll still get your forms. If you want to not have Windows Forms, you'll need to remove the forms, and replace their functionality with command line arguments or configuration. You can then remove the Windows Forms references, and it'll be converted.

梦途 2024-09-20 05:34:16

不,没有。它很大程度上取决于程序最初是如何编写的。如果最初的程序员使系统相当模块化,那么这不会花费您太长时间。如果所有逻辑都在 WinForms 代码隐藏中,那么您需要做一些认真的工作。

这是手动编程/重构工作。

No, there is not. It's extremely dependent upon how the program was originally written. If the original programmer made the system fairly modular, this shouldn't take you too long. If all of the logic is in the WinForms code-behind, you have some serious work to do.

It's manual programming/refactoring work.

夏雨凉 2024-09-20 05:34:16

当我必须这样做时,我创建一个空白的 WinForm 和空白的 Console 项目,然后比较差异。差异很小。我在 Visual Studio 中将所有项目作为 XML 文件打开,然后根据差异根据需要进行搜索和替换。效果很好。

然后,我打开每个项目并修复所有错误,例如缺少入口点方法等。

此外,您还可以录制一个临时宏来执行一些平凡的操作。


根据评论进行更新

“Windows 窗体”和“控制台”应用程序之间的项目文件差异很小。也许我应该使用这份琐碎的工作。

您所需要做的就是将 WinExe 更改为 Exe。如果程序员不能对打开的文件进行简单的搜索和替换而不犯错误,那么他们就不应该编程。

我必须为客户处理 50 多个应用程序。一位顾问告诉他们需要一周的时间才能做出改变。我告诉我的客户,我可以在一个小时内现场完成。这项工作的困难部分是交通堵塞。

我以 XML 格式打开所有项目文件,然后使用“在打开的文档中”搜索和替换来更改项目文件中的 OutputTypeStartupObject

我创建了一个宏,它将添加一个“程序”VB.NET 模块和另一个用于 C# 类的宏,其中包含所需的“主”入口点和应用程序运行代码。

此时,我编写了一个小程序来加载 Visual Studio 2008 中的每个项目。我根据项目是 VB.NET 还是 C# 运行相应的宏。我会修复任何错误,编译,运行应用程序,然后关闭。然后,该小程序将启动另一个包含下一个项目的 Visual Studio。该实用程序最困难的部分是获取所有项目的路径,因为我必须使用 DOS,如 dir /b /s *.vbproj > 所示。 c:\project_listing.txt。

如果 OP 只有少数项目,那么我建议他们打开每个项目并进行所需的更改。

我使用类似的技术将大约 20 个 WinCE/WinForms&Console/VB.NET 项目转换为 Desktop/WinForms&Console/VB.NET 项目,在这些项目中我必须转换包含重大差异的项目和表单文件,尤其是表单文件。

在我看来,你们需要放松一下。十分钟后这些都不再重要了。

When I had to do this, I create a blank WinForm and blank Console project then compared the differences. The differences are minor. I opened all the projects as XML files in Visual Studio then did a search-and-replace as needed based on the differences. Worked well.

I then opened each project and fixed any errors, such as missing entry-point method, and so on.

Also, you can record a temporary-macro to do some of the mundane stuff.


Update Based on Comments

The project file differences between a "Windows Forms" and "Console" application are minor. Maybe I should have used the work trival.

All you need to do is change <OutputType>WinExe</OutputType> to <OutputType>Exe</OutputType>. If a programmer cannot make simple search-and-replace with open files without making a mistake, they shouldn't be programming.

I had to do this with 50+ applications for a client. One consultant told them it would take a week to make the changes. I told my client that I could do it in an hour and on-site. The hard part of this job was getting stuck in traffic.

I opened all project files as XML then changed the OutputType and StartupObject in the project files using search-and-replace "in open documents".

I created a macro that would add a "program" VB.NET Module and another for a C# Class with the needed "main" entry point and application-run code.

Once at this point, I wrote a small program that would load each project in Visual Studio 2008. I ran the appropriate macro depending if the project was VB.NET or C#. I would fix any errors, compile, run the application, then close. The small program with then start another Visual Studio with the next project. The hardest part of the utility was getting the paths to all the projects because I had to use DOS, as in dir /b /s *.vbproj > c:\project_listing.txt.

If the OP only has a handful of projects then I suggest they open each project and make the needed changes.

I have used a similar technique to convert about 20 WinCE/WinForms&Console/VB.NET projects to Desktop/WinForms&Console/VB.NET projects where I had to convert project and form files that contained major differences, especially the form files.

In my opinion, you people need to lighten up. None of this matters ten minutes from now.

夏日浅笑〃 2024-09-20 05:34:16

在 Visual Studio 2015 中,在项目菜单下选择属性,然后在输出类型下更改Windows应用程序的下拉列表到控制台应用程序。构建解决方案。完毕。

In Visual Studio 2015, under the Project menu select Properties then under Output Type change the drop down from Windows Application to Console Application. Build Solution. Done.

初懵 2024-09-20 05:34:16

相当烦人的是,将输出类型Windows应用程序切换到控制台应用程序不再适用于.net core目标(.NET Core 3.0、.NET核心 3.1、.NET 5.0)。控制台永远不会出现。

要恢复此功能,请编辑项目的 .csproj 文件并添加

<DisableWinExeOutputInference>true</DisableWinExeOutputInference>

到属性组。您的 .csproj 文件可能如下所示:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <DisableWinExeOutputInference>true</DisableWinExeOutputInference>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

微软的解释

Quite annoyingly, switching Output type to from Windows Application to Console Application no longer works for .net core targets (.NET Core 3.0, .NET Core 3.1, .NET 5.0). Console just never shows up.

To get this functionality back edit project's .csproj file and add

<DisableWinExeOutputInference>true</DisableWinExeOutputInference>

to the property group. Your .csproj file might then look like this:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <DisableWinExeOutputInference>true</DisableWinExeOutputInference>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

Microsoft's explanation

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