Visual Studio 2015 - 发布时更改设置或变量

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

是否可以在发布应用程序时更改设置或变量

或者是否存在某种条件 运行 IF THEN 反对?

例如,我想在发布时更改日志文件的写入方式,但我经常忘记在发布时进行更改

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>

然后在应用程序中,您可以像这样请求变量:

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

并且它会根据您在发布时选择的任何发布配置文件而改变:)