创建“控制台”和“窗口” Visual Studio项目中的配置

发布于 2024-09-08 19:51:18 字数 260 浏览 1 评论 0原文

我在 Visual Studio 2008 中有一个项目。我想要有两种不同的方式来运行它 - 要么作为常规 Windows 应用程序(这样它有一个窗口,我可以执行图形操作),要么作为控制台应用程序(这样它就不会运行)没有窗户,也不做图形)。有没有办法设置它,以便我可以在运行它时使用命令行选项或其他选项在这两个选项之间切换?有没有办法在同一个 Visual Studio 项目中设置两种不同的配置,以便我可以构建任一配置?或者我是否需要创建两个完全独立的项目,一个用于窗口应用程序,一个用于控制台应用程序。

I have a project in Visual Studio 2008. I want to have two different ways of running it - either as a regular Windows application (so it gets a window and I can do graphics stuff) or as a console application (so that it doesn't have a window and doesn't do graphics). Is there a way to set it up so that I can use a command line option or something to switch between these two options when I run it? Is there a way to set up two different configurations in the same Visual Studio proejct so I can build either one? Or do I need to create two completely separate projects, one for the window application and one for the console application.

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

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

发布评论

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

评论(4

瞳孔里扚悲伤 2024-09-15 19:51:18

是的,您可以使用配置管理器创建新配置。每个配置的项目设置可能不同。因此,您可以创建两个配置,并在项目设置中将输出类型设置为“Windows 应用程序”或“控制台应用程序”。

操作方法如下:在“构建类型”下拉列表中(默认情况下具有“调试”和“发布”选项),选择“配置管理器”。添加新配置并将其命名为“调试(控制台)” - 选择此新配置后,转到项目属性并将输出类型设置为控制台应用程序。重复添加 Windows 窗体配置。

Yes, you can use Configuration Manager to create new configurations. The project settings can be (is) different per configuration. So you can create two configurations, and in project settings set Output Type to be "Windows Application" or "Console Application".

This is how you do it: In the Build Type drop down (The one with Debug and Release options by default), select "Configuration Manager". Add a new configuration and call it "Debug (Console)" - with this new configuration selected, go to project properties and set Output Type to Console Application. Repeat to add a Windows Forms configuration.

此生挚爱伱 2024-09-15 19:51:18

推荐的(非 hacky)方法是将所有逻辑放入类库中,然后为控制台和窗口应用程序创建两个单独的项目。然后按照 Chris 的建议添加类库作为这些项目的引用。

您不能拥有一个既充当控制台又充当窗口应用程序的应用程序。原因基本上是控制台和窗口应用程序在二进制级别上不同,PE 标头中的标志不同。

不过,有一些技巧可以让您使用 Windows API 函数(AttachConsoleCreateConsole)来伪造此类行为。详情请参阅

C#:是否可以让单个应用程序根据开关充当控制台或 Windows 应用程序?

但请注意,可能有 与常规控制台或窗口应用程序的行为存在某些差异

The recommended (non-hacky) way to do this would be to place all your logic in a class library and then create two separate projects for a Console and a Window application. Then add the class library as a reference to these projects as suggested by Chris.

You cannot have a single application that both acts as a Console and a Window application. The reason basically is that Console and Window application are different on the binary level with different flags in the PE header.

There are some hacks though that allow you to fake such a behaviour using Windows API functions (AttachConsole and CreateConsole). For details see

C#: Is it possible to have a single application behave as Console or Windows application depending on switches?

But be aware that there might be certain differences in behaviour to regular Console or Window applications.

北渚 2024-09-15 19:51:18

您可能需要考虑将项目分成三个。

  1. 完成这项工作的程序集/dll(取决于您的语言)。
  2. 控制台应用程序。
  3. 一个 Windows 应用程序。

后两者只是程序集/dll 上完成所有工作的两个不同接口。

You might want to consider splitting your project into three.

  1. An assembly/dll (depending on your language) that does the work.
  2. A console application.
  3. A Windows application.

The latter two are just two different interfaces on the assembly/dll that does all the work.

堇年纸鸢 2024-09-15 19:51:18

就像您拥有默认的调试和发布配置一样,您可以创建自己的配置。
右键单击该项目,选择“配置管理器”,单击“活动解决方案配置”下拉框并选择“新建...”。输入“CmdLine Debug”和“Windowed Release”等名称,删除其他配置。现在右键单击该项目,选择“属性”,然后调整适当配置所需的设置。

让您的生活更轻松:将所有配置通用的属性放入属性表中,并在任何地方使用这些表。这样,如果您想要更改适用于所有配置的选项,您只需执行一次。

just like you have the default Debug and Release configurations, you can create your own.
Right-click the project, select 'Configuration Manager', click the 'Active Solution Configuration' dropdown box and select 'New..'. Enter names like 'CmdLine Debug' and 'Windowed Release' etc, delete the other configurations. Now right-click the project, select Properties, and adjust the needed settings for the appropriate configuration.

Tp to make your life easier: put properties that are common to all configuration in property sheets, and use those sheets everywhere. This way, if you want to change an opion that applies to all configurations, you'll only have to do it once.

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