控制台应用程序的多个配置文件

时间:2011-01-10 14:19:05

标签: c# visual-studio-2010 config

已经有similar topic,但它没有回答我的问题。

在visual studio 2010中开发Web应用程序时,您可以拥有多个配置文件。您有默认的Web.config,然后您可以拥有Web.Debug.config和Web.Release.config。

现在让我们构建一个C#控制台应用程序并将其命名为Foo。默认配置文件应为Foo.exe.Config。 我们可以为不同的环境覆盖此配置文件吗? 我们该如何称呼它? Foo.exe.Release.config或Foo.Release.exe.config?

3 个答案:

答案 0 :(得分:4)

查看此帖子: http://www.olegsych.com/2010/12/config-file-transformation/ 演示如何针对不同的环境使用不同的配置。

答案 1 :(得分:3)

VS2010中没有本机支持,但您可以查看此博客: http://vishaljoshi.blogspot.com/2010/05/applying-xdt-magic-to-appconfig.html

答案 2 :(得分:2)

本文以8个步骤解释如何执行此操作:https://mitasoft.wordpress.com/2011/09/28/multipleappconfig/

它解决了我的问题。

更新:如果文章被移除(thx @ A.V :))

,我在下面添加步骤
  1. 准备项目并添加 app.config app.debug.config app.release.config 。确保在 .Net 4.0 下运行。

  2. 右键单击该项目,单击“卸载项目”,然后单击“编辑.csproj”。

  3. 在最后一个 PropertyGroup 下方添加以下内容:

  4. <PropertyGroup>
      <ProjectConfigFileName>App.config</ProjectConfigFileName>
    </PropertyGroup>
    
    1. 修改与app.config / app。*。config files
    2. 相关的 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>
         </None>
       </ItemGroup>
      
      1. 在最后一个导入标记下方插入此
      2.   

        &LT;进口   项目=&#34; $(MSBuildExtensionsPath)\微软\ VisualStudio的\ V10.0 \网络\ Microsoft.Web.Publishing.targets&#34;   /&GT;

        1. 在Project标记结束之前添加此
        2. <Target Name="AfterBuild">
            <TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)"
          
               

          目的地=&#34; @(AppConfigWithTargetPath-&GT;&#39; $(OUTDIR)%(TARGETPATH)&#39;)&#34; /&GT;       

          1. 现在您可以保存项目,右键单击项目并选择Reload Project。

          2. 对于app.debug.config / app.release.config文件,您可以使用为Web项目提供的模板,如下所示

          3. <?xml version="1.0"?>
            
            <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
            
            <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
            
              <appSettings>
                <add key="Mode" value="Debug" xdt:Transform="Insert"/>
              </appSettings>
              <!--
                In the example below, the "SetAttributes" transform will change the value of 
                "connectionString" to use "ReleaseSQLServer" only when the "Match" locator 
                finds an atrribute "name" that has a value of "MyDB".
            
                <connectionStrings>
                  <add name="MyDB" 
                    connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
                    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
                </connectionStrings>
              -->
              <system.web>
                <!--
                  In the example below, the "Replace" transform will replace the entire 
                  <customErrors> section of your web.config file.
                  Note that because there is only one customErrors section under the 
                  <system.web> node, there is no need to use the "xdt:Locator" attribute.
            
                  <customErrors defaultRedirect="GenericError.htm"
                    mode="RemoteOnly" xdt:Transform="Replace">
                    <error statusCode="500" redirect="InternalError.htm"/>
                  </customErrors>
                -->
              </system.web>
            </configuration>