4

I have a C# app with a few different build configurations for different 'flavors' of the app. I would like to have ClickOnce publish options for each configuration, rather than have to set them each time I do a build-and-publish. It appears that the publish options apply to all the configurations. Is there a way to make publish options specific to each configuration?

3 Answers3

2

You cannot make them different for each configuration directly inside Visual Studio. You need to edit the .csproj file and make the following changes to the things you need to be different.

E.g. the first <PropertyGroup> element contains the <PublishUrl> element. You can apply conditions and add the element multiple times:

<PublishUrl Condition=" '$(Configuration)' == 'Debug' ">debug\</PublishUrl>
<PublishUrl Condition=" '$(Configuration)' == 'Release' ">release\</PublishUrl>

When the project is published using the Debug configuration, it will be published to the debug directory. When the project is published using the Release configuration, it will be published to the release directory.

Apply this conditions to the elements you need to modify. Sadly, you can apply the condition to a whole PropertyGroup, but this is much more complex, as you need to duplicate and keep track of both groups, if you make a change in any of them.

BUT:

This will be overwritten every time you save the project from inside Visual Studio. So you can either make a xslt file which transforms the file before publishing, or directly create your own publishing application, that fills all required publishing parameter only when publishing it e.g. from a build server.

Stefan Over
  • 5,851
  • 2
  • 35
  • 61
0

Edit

This answer will not help. Since I wrote it, I found out that even when using the Choose element, the Visual Studio IDE (at least VS2015) can't seem te keep its hands off our configuration. Although the code below works, when switching between configurations and publishing, at some point the IDE will overwrite this section in the project file in such a way that it no longer works as intended.

Because it technically works as is, and as such could perhaps be useful when using another tool for publishing (or when the issue in VS gets fixed), I'll leave the original answer for reference.

Original answer

I too first tried the solution mentioned by Herdo, only to run into the big "But" he mentions; Visual Studio overwriting your conditionals every time you save the project file.

However, I since found that with the Choose element you can achieve the same behavior without Visual Studio messing with it. You still need to manually edit the .csproj file though.

In short, do not apply the Conditional directly to the PublishUrl tag in the first property group. It will get overwritten by Visual Studio. Instead, define another one later in the project file. When building or publishing, the second one's value will supersede the first one's value, but the IDE will leave it alone. It is that second one you can make conditional using a Choose group.

Below that first property group, add section like this:

  <Choose>
    <When Condition=" '$(Configuration)' == 'Debug' ">
      <PropertyGroup>
        <PublishUrl>your\debug\publish\location</PublishUrl>
      </PropertyGroup>
    </When>
    <When Condition=" '$(Configuration)' == 'Release' ">
      <PropertyGroup>
        <PublishUrl>your\release\publish\location</PublishUrl>
      </PropertyGroup>
    </When>
  </Choose>

Of course, you can add as many configurations and flavors as you like.

Community
  • 1
  • 1
M-Peror
  • 712
  • 8
  • 16
-2

At the menu where you set Debug, Release. You can create a new type of configuration. Once you created your own configuration. You can use the code above:

#if ReleaseConfiguration1
//specifics configuration 1 goes here
#endif
#if ReleaseConfiguration2
//specifics configuration 2 goes here
#endif