29

Is it possible to change the assembly name based on the project configuration?

I have tried conditional pragmas on the assemblyinfo.cs file, but that only changes the assembly attributes, not the name itself.

Rob Hunter
  • 2,787
  • 4
  • 35
  • 52

1 Answers1

49

If you right click on your project and choose "Edit Project File" (I'm in 2008 here and it may be a new option, if it is then just open the project file in any old text editor) you should see something similar to the following:

  <PropertyGroup>
    ...
    <AssemblyName>ClassLibrary1</AssemblyName>
    ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
  </PropertyGroup>

Basically any properties that aren't overriden in a more specific property group are inherited from the more general, first group. So to achieve what you want just edit the file so that the AssemblyName tag is defined in each of the specific groups:

  <PropertyGroup>
    ...
    <AssemblyName>ClassLibrary1</AssemblyName>
    ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    ...
    <AssemblyName>ClassLibrary1Debug</AssemblyName>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    <AssemblyName>ClassLibrary1Release</AssemblyName>
  </PropertyGroup>

This will change the assembly name on a per config basis.

Martin Harris
  • 28,277
  • 7
  • 90
  • 101
  • Thanks, Martin. The "Edit Project File" context option looks to be connected to this addon: http://code.msdn.microsoft.com/PowerCommands – xyz Jul 07 '09 at 16:43
  • Works very well! I wish the Visual Studio 2005 interface had allowed this directly. – Rob Hunter Aug 14 '09 at 15:40
  • Curiously the C++ interface has a very powerful system for things like this (property sheets), and UI that makes it doable even if tricky. I really miss property sheets in C#... – Roman Starkov Apr 04 '10 at 22:41
  • 1
    The project file is the file with extension `.csproj`, for those wondering. – JYelton Mar 05 '13 at 21:24
  • 1
    Has anyone used this in a live project over time? I know this is technically possible but are there any side effects where Visual Studio isn't expecting this and it causes problems? Obviously if the dev edits the assembly name in the UI the behavior will be undefined. Is there anything else? I have seen on other forums mention of debugging issues. I need this for having test and prod versions of ClickOnce app on dev workstations. – MikeJansen Nov 17 '15 at 14:27
  • 1
    @MikeJansen: I use it in a live project where i build the 32 and 64 bit versions to the same directory in a batch build. since a `.csproj` is actually a msbuild project file, and msbuild supports this, really the only issue is the UI like you say. other devs editing this is pretty much a non-issue because they have no reason to change it, and it's also something that's easy to catch and revert in source control since commits are reviewed. – caesay Jun 06 '16 at 16:08
  • 2
    @caesay I'm finding the same. I've had it in place for about 6 months now. We do have two other developers in on the project file and I've had a few incidents but like you said, nothing I can't overcome with the help of source control. I will say that these project properties only seem to be calculated on project load. If you change your configuration, you have to close and re-open the project/solution in order for them to take affect. That has bitten me a couple times. – MikeJansen Jun 06 '16 at 19:39
  • On VS 2017 and the only weirdness I've found is that if you've come here because of ClickOnce, you may find you need to choose your configuration and then reload the solution. – Keenan Oct 31 '17 at 20:27