问题描述
我有一个主要的可执行文件,它根据保存在配置文件中的设置运行.我希望能够通过不同的可执行文件更改配置文件中的设置.
I have a main executable that runs based on settings saved in a configuration file. I want to be able to change the settings in the config file through a different executable.
是否有一种简单的方法可以在一个 Windows 窗体项目中构建这两个不同的可执行文件?这意味着当我按 build 时,会在同一个解决方案文件夹中创建两个不同的 EXE 文件 - 一个更改配置文件,另一个使用它.
Is there an easy way of building these two different executables in one Windows Forms project? Meaning that when I press build, two different EXE files get created in the same solution folder - the one that changes the configuration file, and the other that uses it.
我知道如果我创建两个使用相同解决方案文件夹的单独项目,这是可能的,但我希望一步完成.
I know that this is possible to do if I create two separate projects that use the same solution folder, but I was hoping to do it all in one step.
我假设要做到这一点,我需要一个具有两个主要"功能的项目.这可能吗?
I am assuming that to do this, I need a project with two "Main" functions. Is this possible?
推荐答案
您可以根据需要在一个解决方案中构建任意数量的程序集.程序集可能会生成 DLL 文件或 EXE 文件.
You can build as many assemblies in one solution as you like. Assemblies can result in DLL files or EXE files.
创建解决方案(或打开现有解决方案).
Create a solution (or open an existing solution).
在解决方案资源管理器中右键单击根节点并选择添加 →新建项目并选择您要添加的项目类型.
Right-click the root node in Solution Explorer and choose Add → New Project and choose the project type you like to add.
在解决方案资源管理器中右键单击项目项并选择属性 →构建 →输出路径.设置到所需的目录来构建它.对其他项目重复此操作.
Right-click the project item in Solution Explorer and choose Properties → Build → Output path. Set to the desired directory where to build it to. Repeat this for the other projects.
通过这种方式,您可以在解决方案资源管理器中获得以下内容:
This way you get the following in Solution Explorer:
- 我的解决方案
- MyCommonCode(类库,生成 MyCommonCode.dll)
- MyMainApp(Windows 窗体应用程序,生成 MyMainApp.exe)
- MyConfigApp(Windows 窗体应用程序,结果在 MyConfigApp.exe)
MyCommonCode 程序集包含两个 EXE 文件都使用的共享代码,例如配置文件的标识符等.
The MyCommonCode assembly contains shared code that both EXE files are using like the identifiers of your configuration file, etc.
MyMainApp 是主应用程序的 GUI 应用程序(Windows 窗体、WPF 等),其中包含对 MyComonCode 项目的项目引用.
MyMainApp is the GUI application (Windows Forms, WPF, etc.) for your main application with a project-reference to the MyComonCode project.
MyConfigApp 是一个 GUI 应用程序,用于通过对 MyCommonCode 项目的项目引用来编辑配置值.
MyConfigApp is a GUI application for editing the configuration values with a project reference to MyCommonCode project.
构建解决方案后,您将获得以下二进制文件:
MyCommonCode.dll
、MyMainApp.exe
和MyConfigApp.exe
.After building your solution you get the following binaries:
MyCommonCode.dll
,MyMainApp.exe
, andMyConfigApp.exe
.根据评论更新:
一次编译运行只能为每个项目构建一个二进制文件(DLL 或 EXE).您可以执行类似于上面的答案的操作:将大部分代码移动到一个公共/核心 DLL 中,并为两个 EXE 文件制作两个瘦项目,这些项目仅配置和使用"中央公共/核心 DLL 文件.
One compile-run can build only one binary (DLL or EXE) per project. You can do something like the answer above: move most of the code in a common/core DLL and make two thin projects for the two EXE files which only "configure and use" the central common/core DLL file.
您可以使用编译器定义基于同一项目构建不同的 EXE 文件.您甚至可以定义自己的定义.但是每次编译运行,您只能为每个项目构建一个二进制文件(DLL、EXE)——一个或另一个,但不能同时构建.
You can build different EXE files based on the same project using compiler defines. You can even define your own defines. But per compile-run you can only build one binary (DLL, EXE) per project - one or the other, but not both.
这篇关于如何从一个 Visual Studio 项目创建两个不同的可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!