如何在Visual Studio中从同一项目同时构建两个不同图标版本的EXE
Absolutely feasible! You don't have to manually switch build constants and run builds twice—here are two straightforward approaches to set up automatic simultaneous builds for both your standard and custom icon versions:
Approach 1: Use Multiple Build Configurations (Visual Studio UI-Friendly)
This is the most intuitive method if you prefer working within Visual Studio's graphical interface:
- Open your project's Properties (right-click the project > Properties).
- Go to the Configuration Manager (found in the dropdown at the top of the properties window, next to "Platform").
- Create a new solution configuration:
- Click the dropdown under "Active solution configuration" and select
<New...>. - Name it something like
Release_Other, base it on your existingReleaseconfiguration, and check the box to "Create new project configurations".
- Click the dropdown under "Active solution configuration" and select
- For the new
Release_Otherconfiguration:- Switch to the Build tab in project properties.
- Add
OTHER_VERSIONto the Conditional compilation symbols field (separate from existing symbols with a semicolon if needed).
- Configure output paths to avoid overwrites:
In each configuration's Build > Output settings, set a unique output directory (e.g.,bin\Release\Standardfor the default config,bin\Release\Otherfor the new one). - Trigger the simultaneous build:
Back in Configuration Manager, check the "Build" box for both your original configuration (e.g.,Release) and the newRelease_Otherconfiguration. Click Close, then run a solution build—Visual Studio will build both versions in one shot, each using its assigned icon.
Approach 2: Custom MSBuild Targets (Advanced, Flexible)
If you prefer scripting the build process directly in the project file, you can add custom MSBuild targets to trigger both builds with a single command:
- Unload your project in Visual Studio (right-click the project > Unload Project), then edit the
.csproj/.vbprojfile. - Add the following XML at the end of the file (before the closing
</Project>tag):
<Target Name="BuildBothVersions" DependsOnTargets="BuildStandard;BuildOther"> </Target> <Target Name="BuildStandard"> <MSBuild Projects="$(MSBuildProjectFile)" Properties="DefineConstants=;OutputPath=bin\Standard\$(Configuration)" Targets="Build" /> </Target> <Target Name="BuildOther"> <MSBuild Projects="$(MSBuildProjectFile)" Properties="DefineConstants=OTHER_VERSION;OutputPath=bin\Other\$(Configuration)" Targets="Build" /> </Target>
- Reload the project. Now you can:
- Run the custom target from Visual Studio's Command Window with
Build.BuildBothVersions. - Or execute it via command line with
msbuild YourProject.csproj /t:BuildBothVersions.
- Run the custom target from Visual Studio's Command Window with
Your existing ApplicationIcon property group will work perfectly with either approach—MSBuild will pick the correct icon based on whether OTHER_VERSION is defined in each build context.
内容的提问来源于stack exchange,提问作者Andreas Zita

