VisualStudio App.Config自動配置

在VisualStudio中,可以跟據選定的運行模式自動生成相應的配置文件;

對web類型的項目,系統已自動有了Web.config、Web.Debug.config、Web.release.config三個配置,且可自動使用轉換工具自動來實現轉換。

對於像應用程序之類的項目,沒有能自動實現這些,這對於項目的發佈有的時候是個問題。對於這種情況,可以如下配置項目文件以實現相同功能:

1:卸載要編輯的項目,右鍵點點擊“編輯”項目文件對應的xxproj文件。比如xx.csproj

2:在打開的文件裏,按着已有的格式添加如下內容

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

  <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>


  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />


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

3:保存並重新加載項目。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章