For those who do WEB in ASP.Net, they know the principle of having several Config files:
Web.Config
Web.Debug.Config
Web.Release.Config
The Web.Config file is for your own development, the Web.Debug.Config file is for the TEST or DEV version (on another server for example) and the Release version is for the PRODUCTION server.
But for a Console or Winform application, this possibility of having multiple Config files does not exist, at least not at hand. I will show you how to add this option in a Console or Winform or even WPF application.
Follow the steps precisely:
1. Create an App.Debug.Config file or an App.Release.Config for example in Visual Studio below your original App.Config
2. Right-click on your project and click on "Unload Project" (in English), I do not know the French translation but it should be something like "Décharger le Projet"
3. Then on the project, right-click and do "Edit" or "Editer" .csproj or .vbproj
4. Copy these lines below the last PropertyGroup :
<PropertyGroup> <ProjectConfigFileName>App.config</ProjectConfigFileName></PropertyGroup> |
5. Where you find the line <None Include="App.Config" /> (you are looking for this), modify and add the following block <None Include="App.Debug.Config"> in the ItemGroup section, of course if you are using only one additional Config file or several, it's up to you to put these new blocks that correspond to your files and don't forget to change the name:
<ItemGroup> <NoneInclude="App.config"/> <NoneInclude="App.Debug.config"> <DependentUpon>App.config</DependentUpon> </None> <NoneInclude="App.Release.config"> <DependentUpon>App.config</DependentUpon> </None> </ItemGroup> |
6. Below the last Import tag copy this:
<ImportProject="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v15.0\Web\Microsoft.Web.Publishing.targets"/> |
For Visual Studio 2017, this is version v15.0, but if you are using Visual Studio 2019, you replace v15.0 with v16.0 (for VS2015 = v14.0 and for VS2013=v12.0)
You can even find the version of your Visual Studio on your PC here:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v[YOUR VISUAL STUDIO VERSION]\Web\Microsoft.Web.Publishing.targets
7. Add this at the end of your file before the tag </Project>
<TargetName="AfterBuild"> <TransformXmlSource="@(AppConfigWithTargetPath)"Transform="$(ProjectConfigTransformFileName)"Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" /></Target> |
8. Save everything and reload your project
9. Don't forget like in ASP.Net to put the tags xdt:Transform="SetAttributes" xdt:Locator="Match(name)" in your new Config file after each element you want to transform and the tag <configuration> must have the following xdt schema: xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
An example:
<?xmlversion="1.0"?> <appSettings> <addkey="Mode"value="Debug"xdt:Transform="Insert"/> </appSettings> <connectionStrings> <add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> </connectionStrings></ |
Aucun commentaire pour le moment.