问题

部署项目时,常常需要根据不同的环境使用不同的配置文件。例如,在部署网站时可能希望禁用调试选项,并更改连接字符串以使其指向不同的数据库。在创建 Web 项目时,Visual Studio 自动生成了 Web.configWeb.Debug.configWeb.release.config这3个不同的配置文件,并提供了转换工具,用于在部署项目时自动转换配置文件内容。具体可以参考这2篇文章:如何:在部署 Web 应用程序项目时转换 Web.config 和 用于 Web 应用程序项目部署的 Web.config 转换语法 。

然而在其他项目类型中(如控制台应用程序、Windows 服务),并没有现成的配置文件的转换功能。

  1. 我们在项目中添加 App.configApp.Debug.configApp.Release.config 这3个配置文件。

  2. 打开项目所在目录,用记事本或其他文本编辑器打开 .csproj 文件。

  3. 在 PropertyGroup 标签下添加如下内容:

    <PropertyGroup>
    <ProjectConfigFileName>App.config</ProjectConfigFileName>
    </PropertyGroup>

4.在 ItemGroup 标签中找到和 App.configApp.Debug.configApp.Release.config 相关的项目,替换为

<None Include="App.config" />
<None Include="App.Debug.config">
<DependentUpon>App.config</DependentUpon>
</None>
<None Include="App.Release.config">
<DependentUpon>App.config</DependentUpon>
</None>

5.在最后一个 Import 标签后面添加:

<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />

6.在 Import 标签后面添加 Target 标签:

<Target Name="AfterBuild">
<TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
</Target>

7.切换到 Visual Studio , 重新加载项目。

8.这时查看 Visual Studio 可以看到 App.config 的组织方式和 Web.config 一样了。

现在就可以使用 用于 Web 应用程序项目部署的 Web.config 转换语法 这篇文章中提到的转换语法了。

例如需要替换 connectionStrings , App.config 有如下配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="connString" connectionString="Server=debug;Database=test;Uid=root;Pwd=123456;CharSet=utf8;"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>

只需要修改 App.Release.config 为如下内容即可:

<?xml version="1.0" encoding="utf-8"?>

<!-- 有关使用 web.config 转换的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="connString"
connectionString="Server=release;Database=test;Uid=root;Pwd=654321;CharSet=utf8;"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
</configuration>

这样在选择 Release 配置时,connectionStrings 会自动替换成 App.Release.config 中的值。查看 bin\Release 目录下的 config 文件可以进行验证。

完整代码

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{8196CA4E-AD25-4F90-BB80-D27512BF4BD4}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>App.Config转换</RootNamespace>
<AssemblyName>App.Config转换</AssemblyName>
<TargetFrameworkVersion>v4.</TargetFrameworkVersion>
<FileAlignment></FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel></WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel></WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ProjectConfigFileName>App.config</ProjectConfigFileName>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="App.Debug.config">
<DependentUpon>App.config</DependentUpon>
</None>
<None Include="App.Release.config">
<DependentUpon>App.config</DependentUpon>
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<Target Name="AfterBuild">
<TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
</Target>
</Project>

转自:在部署 C#项目时转换 App.config 配置文件

最新文章

  1. php示例代码之读取文件
  2. 网站的SEO
  3. appium for windows 环境搭建
  4. IO流详解之代码详解
  5. Howto Setup yum repositories to update or install package from ISO CDROM Image
  6. android部分控件应用解析
  7. Spring源码解析——如何阅读源码(转)
  8. H264相关随笔
  9. 纯Lambda实现斐波那契数列
  10. 我的第一个Java程序
  11. UITableView 的使用小点
  12. Android开发过程中的坑及解决方法收录(六)
  13. lua table表判断是否为空
  14. python多进程并发和多线程并发和协程
  15. React基础概念
  16. 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神
  17. Verilog有符号数处理
  18. 项目目前展示图 有几个Activity页还没连上不能一次展示出来
  19. Docker容器数据卷
  20. 《图解TCP/IP》读书笔记(转)

热门文章

  1. The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC&#39;s basketball team
  2. 性能测试基础---ant集成1
  3. 使用ftp搭建yum仓库
  4. 201871010110-李华《面向对象程序设计(java)》第四周学习总结
  5. 三块sm865组建RAID0
  6. 转:ubuntu16安装python3.6并将环境设置为系统默认
  7. es4x 使用nodejs 开发vertx 应用框架试用
  8. Vue模板语法(一)
  9. 记录VUE-CLI项目创建及初始化相关
  10. oracle用dba创建用户并授权