Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigurationManager.AppSettings[key] is always null

Tags:

c#

I am trying to get the value for the key "sUser" in appSetting section from app.config

this is the code on my class.cs

string sUsr = ConfigurationManager.AppSettings.Get["sUser"];

But always its null.

I try this code (I took it from MSDN):

// Get the configuration file.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Get the appSettings section.
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");

// Get the auxiliary file name.
Console.WriteLine("Auxiliary file: {0}", config.AppSettings.File);

But it shows that the file Empty

Also I am using the stting console to write those properties and it works if I get the apps using

string sUsr = WIW.Business.Properties.Settings.Default.sUser;

But I am going to use that class as a reference in a website and on the web.config of the site I can't configure those settings

like image 488
Copeleto Avatar asked Apr 01 '26 19:04

Copeleto


2 Answers

In your App.config the appSettings section should look like the following:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="sUser" value="Test"/>
  </appSettings>
</configuration>
like image 150
arc1880 Avatar answered Apr 04 '26 09:04

arc1880


use Properties.Settings.Default.{your_setting_key} to access the value

I.E

Properties.Settings.Default.sPassword return 8909039388

All these were included in Properties Class.

like image 36
xTan Avatar answered Apr 04 '26 07:04

xTan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!