Answered App.config compiled but return null value.

NoviceMer

Member
Joined
Jan 27, 2021
Messages
9
Programming Experience
5-10
Dear Developers,

I would like to clarify my understanding that there is bug with application configuration file provided by Nuget package for System.Configuration.ConfigurationManager. Attempting to follow Microsoft advice but ConfigurationManager.AppSettings["key0"] cannot receive the input.

Below is the code:
XML:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version ="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <appSettings>
        <add key ="key0" value="test"/>
    </appSettings>
</configuration>
At program.cs:
C#:
using System.Configuration;
using System.Collections.Specialized;

static void Main(string[] args)
{
     string sSrvName = ConfigurationManager.AppSettings["Key0"];
}

Your kind sharing is very much appreciated.


Best Regards
 
Last edited by a moderator:
App settings are case sensitive. The app.config has "key0", but you are trying to find "Key0".
 
I would generally recommend your not using that section of the config file anyway. If you add your settings on the Settings page of the project properties then you can access them in code via Properties.Settings.Default. You will then get full Intellisense and strong typing because there is a property created for each setting. That means that you can't make a mistake like you did here.
 
App settings are case sensitive. The app.config has "key0", but you are trying to find "Key0".
Hi,

Thank you for your advice. Rectified but null still persists.

1613527250548.png


Does app.config run alright at your end so far?
 
I would generally recommend your not using that section of the config file anyway. If you add your settings on the Settings page of the project properties then you can access them in code via Properties.Settings.Default. You will then get full Intellisense and strong typing because there is a property created for each setting. That means that you can't make a mistake like you did here.
Those settings are still stored in the config file, so they can be modified by hand after deployment, but in a different section.
 
.NET Core 2.0. Yep, not Framework.
For future reference, that is important information because most people will assume .NET Framework unless otherwise specified. The fact that you mentioned a NuGet package made me suspicious but some may not have noticed.
 
I just tested a .NET Core 3.0 app and it worked fine for me.
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="key0" value="test"/>
  </appSettings>
</configuration>
Program.cs:
using System.Configuration;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var key0 = ConfigurationManager.AppSettings["key0"];
        }
    }
}
key0 contained the value "test" when I debugged that.
 
Those settings are still stored in the config file, so they can be modified by hand after deployment, but in a different section.

Those settings are still stored in the config file, so they can be modified by hand after deployment, but in a different section.
For future reference, that is important information because most people will assume .NET Framework unless otherwise specified. The fact that you mentioned a NuGet package made me suspicious but some may not have noticed.
I greatly appreciate your advice. I note it.

Pardon my shallow understanding, is it not encouraging to use app.config? I did not enable intellisense. I have not explored the properties.Settings.Default.

I am looking for other alternatives to read config file line by line and store it as variable.
 
Really?

If you remembered me, i did post about my trouble in using Visual Studio 2019. I finally downgrade to VS 2017 due to time constraint. So, applicable .NET Core is 2.0.
 
I would generally recommend your not using that section of the config file anyway. If you add your settings on the Settings page of the project properties then you can access them in code via Properties.Settings.Default. You will then get full Intellisense and strong typing because there is a property created for each setting. That means that you can't make a mistake like you did here.
It appears that application settings are not available in .NET Core Console Application projects, so you can scratch that suggestion.
 
Back
Top Bottom