Search code examples
variablesvisual-studio-2015clickoncepublishapplication-settings

Visual Studio 2015 - Change Setting or Variable at time of Publish


Is it possible to change a setting or variable when the application is published

or is there some sort of condition to run an IF THEN against?

for example, I want to change the way the log files are written when I publish and I often forget to make the change when I publish it


Solution

  • Web.Live.Config:

    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <!--
        In the example below, the "SetAttributes" transform will change the value of
        "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
        finds an attribute "name" that has a value of "MyDB".
     -->
      <appSettings>
        <add key="ClaimPackPath" value="C:\\inetpub\\wwwroot\\Application\\ClaimPacks\\" xdt:Locator="Match(key)" xdt:Transform="Replace" />
    </appSettings>
    </configuration>
    

    Wg.Debug.Config:

    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <!--
        In the example below, the "SetAttributes" transform will change the value of
        "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
        finds an attribute "name" that has a value of "MyDB".
     -->
      <appSettings>
        <add key="ClaimPackPath" value="C:\\Debug\\wwwroot\\Application\\ClaimPacks\\" xdt:Locator="Match(key)" xdt:Transform="Replace" />
    </appSettings>
    </configuration>
    

    Then in the application you can request the variable like so :

    string filepath = ConfigurationManager.AppSettings["ClaimPackPath"];
    

    And it will change for whatever publish profile you choose at time of publish :)