Change the icon of the executable file depending on the selected build configuration

Page update date :
Page creation date :

environment

framework
  • Windows Forms (.NET Framework) in general
  • Windows Forms (.NET) General
  • WPF (.NET Framework) in general
  • WPF (.NET) in general

At first

Normally, only one icon can be set in an EXE project. This section describes how to switch the icon for EXE files created by the selected build configuration.

The program we are raising as a procedure starts with the one with the project newly created. It is also possible to set it for projects that you have already built.

Here, the icon is set to change depending on the build of "Debug" and "Release" configured in the initial state. Also, as described in the Windows Forms (.NET) project as an example, the procedure is the same for the other frameworks listed at the beginning.

Preparing an icon file

This time, we have prepared an icon like the figure as a reference.

Icon File Settings

Register Visual Studio to be able to apply icons to EXE files in the usual way.

For .NET (Core)

The first icon file is added to the project.

Follow the same steps to select the second icon.

A second icon file is added to the project.

The setting of the first icon will be overwritten, but please ignore it because the setting on the editor is not relevant this time. It is important that there are two icons registered in the project.

For the .NET Framework

The first icon file is added to the project.

Follow the same steps to select the second icon.

A second icon file is added to the project.

The setting of the first icon will be overwritten, but please ignore it because the setting on the editor is not relevant this time. It is important that there are two icons registered in the project.

Check the build configuration

Check the build configuration in the toolbar.

Use the name of the build configuration shown here as the icon switching decision. In the initial project, there are "Debug" and "Release", so we will use this.

Open the code in the project file

Right-click the project file and choose Edit Project File.

This menu is only available for .NET (Core) projects. NET Framework projects, open the .csproj file in a text editor.

Edit the code in the project file

The .NET (Core) project is described here as an example, but the . NET Framework, the procedure is the same.

I think the code for the .NET project looks like this: (It's an example, so it actually depends on the project.)

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
    <ApplicationIcon>2.ico</ApplicationIcon>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="2.ico" />
  </ItemGroup>

</Project>

The value of in PropertyGroup > ApplicationIcon this is the icon file applied to the executable. These tags can be conditionalized, and to change the application of the icon depending on the build configuration, modify the part as ApplicationIcon follows:

<ApplicationIcon Condition=" '$(Configuration)' == 'Debug' ">1.ico</ApplicationIcon>
<ApplicationIcon Condition=" '$(Configuration)' == 'Release' ">2.ico</ApplicationIcon>

ConditionYou can include conditions in the parameters, where contains$(Configuration) the name of the build configuration at the time of the build. The condition is that if is and if is Debug 1.icoRelease used 2.ico for . If you have renamed the build configuration in your project, rename it Debug Release . If you want to increase the icon, increase the tag.

All that's left is to save the code and build it with the desired build configuration.

Build Results

The executable file built with Debug looks like this:

The executable file built with Release looks like this: