2011-01-10 36 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> 
    
    相關問題