ConfigurationManager return null instead of string values

31,721

Solution 1

Did you ensure that the config file is placed correctly at the directory from which you're running the application? Is there actually a file called <app name>.exe.config in that directory?

I'm just guessing here - maybe you added the App.Config file in a different project then your exe assembly project...?

By the way, I copied your code and App.Config as is to a clean project, and this code worked for me. So I'd look in the direction of the config file itself and not in the code. The code is fine...

Hope this helps,

Ran

Solution 2

If your config file use in different class library you must change your name YourClasslibraryDllname.dll.config and you must change config file copy to output directory property

Ex:
   YourSolution
       ClassLibrary_1
            ClassLibrary_1.dll.config
            ApplicationConfigurationReader.cs
            ConfigurationConst.cs
       ClassLibrary_2
       ConsoleApp
  1. Rename your config file like this YourClasslibraryDllname.dll.config
  2. Open Properties Window
  3. Change Do Not Copy to Copy Always

enter image description here

  1. Add reference -> Assembly -> System.Configuration

  2. Add below clases in ClassLibrary_1 Project

ConfigurationConst Class using System.Configuration;

public static class ConfigurationConst
{
   public static KeyValueConfigurationCollection Configs;
}

ApplicationConfigurationReader class using System.Configuration;

internal class ApplicationConfigurationReader
    {
        public void Read()
        {
            // read assembly
            var ExecAppPath = this.GetType().Assembly.Location;

            // Get all app settings  in config file
            ConfigurationConst.Configs = ConfigurationManager.OpenExeConfiguration(ExecAppPath).AppSettings.Settings;

        }
    }

Read Config using ClassLibrary_1;

static void Main(string[] args)
{
    new ApplicationConfigurationReader().Read();
    var Configval = ConfigurationConst.Configs["provider"].Value;            
    Console.ReadKey();
}

i Hope you can get clean help

Share:
31,721
TheBoss
Author by

TheBoss

Updated on July 09, 2022

Comments

  • TheBoss
    TheBoss almost 2 years

    I am trying to retrieve values from my App.config file which is stored in my working directory, however when I run the program it returns null. I am very confused why this is so, and have looked over the code many times in an attempt to spot an error.

    Here is my App.config file code:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="provider" value="System.Data.SqlClient" />
      </appSettings>
      <connectionStrings>
        <add name="connection" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=Autos;Integrated Security=True;Pooling=False" />
      </connectionStrings>
    </configuration>
    

    Here is my C# code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using System.Data;
    using System.Data.Common;
    
    namespace DataProviderFun
    {
      class Program
      {
        static void Main(string[] args)
        {
          string p = ConfigurationManager.AppSettings["provider"];
          string c = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
    
          ...
    

    When I run this code, p = null and c = null.

    I have referenced System.Configuration.dll.

  • TheBoss
    TheBoss over 13 years
    I put the App.config file in my bin\Debug folder, however this folder, and none of the other folders associated with this project contain an <app>.exe.config file. This is an executable, not a library. I've tried cleaning and rebuilding the project but this file does not appear.
  • Ran
    Ran over 13 years
    You should not manually copy the App.config file. It is supposed to be copied and renamed automatically at build time. Maybe you just added a file called App.config to your project, instead of adding an "application configuration" item? You can try re-adding it to the executable project.
  • TheBoss
    TheBoss about 12 years
    "Maybe you just added a file called App.config to your project, instead of adding an "application configuration" item" is the correct answer. Thanks Ran.
  • Amit Sharma
    Amit Sharma over 2 years
    This solution worked for me. And I did not have to rename the config file to use YourClasslibraryDllname.dll.config and simply had the name "App.config".