Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change exe icon based on define C#

Tags:

c#

icons

exe

I am creating a project for two different people and I would like to change the icon via define. For example:

#if customer1
//add code to select c:\path to resources\myimage1.ico for exe icon
#else
//add code to select c:\path to resources\myimage2.ico for exe icon
#endif

I know you can manually select which icon you want here:

https://msdn.microsoft.com/en-us/library/339stzf7.aspx

But the define way make sense for us using git so we don't have to keep reuploading someone else's image. We can simply just put the define and have it use that image. Thanks.

like image 566
fac7orx Avatar asked Oct 06 '15 18:10

fac7orx


1 Answers

You can modify the csproj file so that you create different build configurations for the two customers. For example, you can do the following:

  1. Unload the project by right-clicking on the project in the Solution Explorer within Visual Studio and clicking "Unload Project".

  2. Right-click on your unloaded project and click "Edit "

  3. Find the "ApplicationIcon" tag and replace it with two conditional PropertyGroups, like this:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer1|AnyCPU' ">
  <ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer2|AnyCPU' ">
  <ApplicationIcon>netfol.ico</ApplicationIcon>
</PropertyGroup>

This will create a debug build configuration for Customer1 and Customer2.

  1. Do the same on your icon ItemGroup, also in the project file, like this:

<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer1|AnyCPU' ">
  <Content Include="icon.ico" />
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer2|AnyCPU' ">
  <Content Include="netfol.ico" />
</ItemGroup>
  1. Find the PropertyGroup for the configuration, itself. It will have this line:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  1. Below this group, add groups for the debug configuration for your two new debug customer configurations, like this:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer1|AnyCPU' ">
  <PlatformTarget>AnyCPU</PlatformTarget>
  <DebugSymbols>true</DebugSymbols>
  <DebugType>full</DebugType>
  <Optimize>false</Optimize>
  <OutputPath>bin\Debug_Customer1\</OutputPath>
  <DefineConstants>DEBUG;TRACE</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Customer2|AnyCPU' ">
  <PlatformTarget>AnyCPU</PlatformTarget>
  <DebugSymbols>true</DebugSymbols>
  <DebugType>full</DebugType>
  <Optimize>false</Optimize>
  <OutputPath>bin\Debug_Customer2\</OutputPath>
  <DefineConstants>DEBUG;TRACE</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
</PropertyGroup>
  1. Click Save.

  2. Right-click on the project file in the Solution Explorer and click "Reload Project". Say yes if Visual Studio asks if you want to close the project file.

  3. Now when you want to build for different customers, go to Build\Configuration Manager and select the customer-specific configuration. Configuration Manager with Customer-Specific Configuration

  4. Repeat these steps to add release-specific configurations for each customer.
like image 147
Keith Holloway Avatar answered Sep 19 '22 04:09

Keith Holloway