[Visual Studio] 於Solution 內設定不同的app.config

將Project profile 化可以使自己的publish 設定予不同的場地, 從而令設定易客制化同時避免人為錯誤. 方法如下.

  1. 確定沒有其他程式開啟project file(e.g. Visual Studio);
  2. 在Project 的文件夾內, 找出<<Project名稱>>.csproj, 並用文家處理軟件開啟(e.g. Notepad++);
  3. 設定app.config, 對應不同的環境, 加入須要的app.config.
    於最後的<ItemGroup / >內, 加入以下句子:

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

  4. 設定Post-build event, 當Build 好後, 會將指的config 檔改名作app.config.
    在最上的<Import /> 中, 加入以下句子:

    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
      <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
      <Target Name="AfterBuild">
        <TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
      </Target>

  5. 將app.config 複制並重新命名. 
  6. 重新開啟Visual Studio, 便會見到config file 已經被Group 起來.
  7. 要進行設定的話, 只須在Solution Property 中選取須要的Configuration 便可.
About C.H. Ling 262 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.