Search code examples
c#visual-studiomsbuildconditional-compilation

Change name of exe depending on conditional compilation symbol


Can you tell Visual Studio to output a different name of an exe file depending on if a specific conditional compilation symbol is set?


Solution

  • Since defining a condition to the assemblyname tag as suggested by Fredrik seems to make Visual Studio cranky, you can change the assembly name later in the csproj file. Using the Choose element is kind of like an if statement, so a name can be appended if a condition is met, demonstrated below.

    Getting a substring out of for instance DefineConstants in a condition attribute does not seem possible (according to MSDN) with "plain vanilla MSBuild", but one can define their own build targets and set a property when compiling with a /p:Tag=value (MSBuild command line reference)

      ...
      <Tag>true</Tag>
    </PropertyGroup>
    <Choose>
      <When Condition=" '$(Tag)' == 'true' ">
        <PropertyGroup>
          <AssemblyName>$(AssemblyName).TagDefined</AssemblyName>
        </PropertyGroup>
      </When>
    </Choose>
    <ItemGroup>
    ...