Several configuration files for console applications

There is already a similar topic , but it does not answer my question.

When developing web applications in visual studio 2010, you can have several configuration files. You have the default Web.config file, and then you can have Web.Debug.config and Web.Release.config.

Now create a C # console application and name it Foo. The default configuration file should be Foo.exe.Config. Can we override this configuration file for different environments? How can we call it? Foo.exe.Release.config or Foo.Release.exe.config?

+3
source share
3 answers

8 , : https://mitasoft.wordpress.com/2011/09/28/multipleappconfig/

.

: , ( @A.V:))

  • app.config, app.debug.config app.release.config. , .Net 4.0.

  • , " ", " .csproj".

  • PropertyGroup :

<PropertyGroup>
  <ProjectConfigFileName>App.config</ProjectConfigFileName>
</PropertyGroup>
  1. ItemGroup, app.config/app. *.
<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)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" /" >

  1. Project
<Target Name="AfterBuild">
  <TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)"

Destination = "@(AppConfigWithTargetPath → '$ (OutDir)% (TargetPath)')" /" >     

  1. , " ".

  2. For the files app.debug.config / app.release.config you can use the template that is provided for web projects, which looks like this

<?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>
+1
source

Source: https://habr.com/ru/post/1784540/


All Articles