2011-01-10 76 views
3

目前已经是一个similar topic但它不回答我的问题。多个配置文件控制台应用程序

在开发Visual Studio 2010中的Web应用程序,你可以有多个配置文件。你有默认的Web.config文件,然后你可以有一个Web.Debug.config和Web.Release.config。

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

回答

1

这篇文章解释了8个步骤如何做到这一点:https://mitasoft.wordpress.com/2011/09/28/multipleappconfig/

它解决了这个问题对我来说。

更新:我添加下面的步骤的情况下,如果文章被删除(感谢@AV :))

  1. 准备好你的项目,并添加的app.configapp.debug.configapp.release.config。确保下.NET 4.0运行。

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

  3. 低于最终的PropertyGroup添加以下:

<PropertyGroup> 
    <ProjectConfigFileName>App.config</ProjectConfigFileName> 
</PropertyGroup> 
  • 修改部分的ItemGroup该对相关的app.config /app.*.config文件
  • <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> 
    
  • 下面最后一个导入标签插入这个
  • <进口 项目=“$(MSBuildExtensionsPath)\微软\ VisualStudio的\ V10 .0 \网络\ Microsoft.Web.Publishing。目标 “ />

  • 右键添加此
  • <Target Name="AfterBuild"> 
        <TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" 
    

    目的地=” @(AppConfigWithTargetPath - >'$( OUTDIR)%(TARGETPATH)')” />

  • 现在您可以保存该项目,右键单击该项目并选择重新加载项目。

  • 对于app.debug.config/app.release.config文件,你可以使用商提供的用于Web项目的模板,它看起来像下面这样

  • <?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> 
    
    相关问题