Read app.config values in .Net 6 console

Salam 26 Reputation points
2022-06-28T13:40:00.27+00:00

Can somebody tell me how can I read my connection strings in ths app.config file in a .net 6 console

<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
	<connectionStrings>  
    <add name="ConnStr1"  
        connectionString="Data Source=MySqlK12;Initial Catalog=admin;Integrated Security=True;Encrypt=False;Application Name=EasyCompareDB;"  
        providerName="System.Data.SqlClient" />  
	<add name="ConnStr2"  
        connectionString="Data Source=SecondK1;Initial Catalog=admin;Integrated Security=True;Encrypt=False;Application Name=EasyCompareDB;"  
        providerName="System.Data.SqlClient" />  
  </connectionStrings>   
</configuration>  

Thanks

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,370 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
322 questions
{count} vote

4 answers

Sort by: Most helpful
  1. bkwdesign 11 Reputation points
    2022-09-18T05:23:48.753+00:00

    I have a unit test project that I coverted to SDK format, and I want it to support both Framework 4.8 and .NET 6.0
    So, my legacy code that I transitioned into this format, already features an app.config file.

    The tests would build successfully, properly duplicated for each framework target, and the 4.8 tests would pass, but the 6.0 tests couldn't find a connection string

    I had a PackageReference for: System.Configuration.ConfigurationManager version 6.0.1

    The root of the problem is that when building the project, the app.config is not transformed into MyCorp.MyTest.exe.config

    I finally found this snippet over on this GitHub thread:

    #if NETCOREAPP  
    using System.Configuration;  
    using System.IO;  
    using System.Reflection;  
    #endif  
      
    ...  
      
    // In your global setup:  
    #if NETCOREAPP  
        string configFile = $"{Assembly.GetExecutingAssembly().Location}.config";  
        string outputConfigFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;  
        File.Copy(configFile, outputConfigFile, true);  
    #endif  
    
    2 people found this answer helpful.
    0 comments No comments

  2. Bruce (SqlWork.com) 55,686 Reputation points
    2022-06-28T15:37:05.67+00:00

    net core typically uses json files instead of xml:

    https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration

    if you need xml support, you will need to manually configure xml support:

    https://www.thecodebuzz.com/loading-configuration-json-ini-xml-net-core/

    0 comments No comments

  3. Salam 26 Reputation points
    2022-06-28T19:58:37.71+00:00

    Sorry Bruce, but when I right-clicked the project in VS 2022, > add configuration file, it added app.config and not json file. It is not a net core it is a .net console application


  4. Salam 26 Reputation points
    2022-06-28T20:02:19.08+00:00

    Also, I visited the 2nd URL you provided, it is mainly for .net core with the old startup and program stuff which is not the case in my .Net 6 standard project

    0 comments No comments