How to use ConfigurationManager.AppSettings with a custom section?

read custom configuration section c#
configurationmanager.appsettings not working
c# read custom config file
how to read app.config file in c# console application
c# app.config custom section
update app.config appsettings programmatically c#
c# app.config multiple values
how to write app.config file in c# windows application

I need to get "http://example.com" from using App.config file.

But at the moment I am using:

string peopleXMLPath = ConfigurationManager.AppSettings["server"];

I cannot get the value.

Could you point out what I am doing wrong?

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <configSections>
    <section name="device" type="System.Configuration.SingleTagSectionHandler" />
    <section name="server" type="System.Configuration.SingleTagSectionHandler" />
  </configSections>
  <device id="1" description="petras room" location="" mall="" />
  <server url="http://example.com" />
</configuration>

I think you need to get the config section, and access that:

var section = ConfigurationManager.GetSection("server") as NameValueCollection;
var value = section["url"];

And you also need to update your config file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <configSections>
    <section name="device" type="System.Configuration.NameValueSectionHandler" />
    <section name="server" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <device>
    <add key="id" value="1" />
    <add key="description" value="petras room" />
    <add key="location" value="" />
    <add key="mall" value="" />
  </device>
  <server>
    <add key="url" value="http://example.com" />
  </server>
</configuration>

Edit: As CodeCaster mentioned in his answer, SingleTagSectionHandler is for internal use only. I think NameValueSectionHandler is the preferred way to define config sections.

Does ConfigurationManager.AppSettings[Key] read from the web , the physical file each time you ask for a value. This is why it is necessary to restart an Windows app (or Refresh the config) to get the latest value, and why an ASP.Net app automatically restarts when you edit web. The appSettings configuration section provides for key/value pairs of string values for an application. Rather than accessing these values by using an instance of an AppSettingsSection object, you should use the AppSettings property of the ConfigurationManager class or the AppSettings property of the WebConfigurationManager class. Constructors

The SingleTagSectionHandler documentation says:

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.

You can retrieve it as a HashTable and access its entries using Configuration.GetSection():

Hashtable serverTag = (Hashtable)ConfigurationManager.GetSection("server");

string serverUrl = (string)serverTag["url"];

Four Ways To Read Configuration Setting In C#, Config or App.Config but we need to add <appSettings> section inside the To access these values, there is one static class named ConfigurationManager, custom section settings, we first need to find out the section, using  To change the configuration, you use the GetSection method on the configuration file that you obtain by using one of the following Open methods: OpenExeConfiguration. OpenMachineConfiguration. OpenMappedExeConfiguration. Notes to Inheritors. You must cast the return value to the expected configuration type.

string peopleXMLPath = ConfigurationManager.AppSettings["server"];

gets the value from the appSettings part of the app.config file but you are storing your value in

<server url="http://example.com" />

Either put the value in the appSettings section as below or retrieve the value from its current location.

You need to add a key value pair to your config's appSettings section. As below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="server" value="http://example.com" />
    </appSettings>
</configuration>

Your reading code is correct but you should probably check for null. If the code fails to read the config value the string variable will be null.

You can use an application configuration file to collect custom application <​appSettings> section of the configuration file, declare a string use the Get method of the AppSettings property of the ConfigurationManager class. First you should inject IConfiguration and then for reading from appsettings, you can use one of this methods: Get a section data. var redisConfig = configuration.GetSection("RedisConfig"); Get a value within a section. var redisServer = configuration.GetValue<string>("RedisConfig:ServerName"); Get nested value within section

You're defining a configuration section instead of a value in AppSettings. You can simply add your setting to AppSettings:

<appSettings>
      ... may be some settings here already
      <add key="server" value="http://example.com" />
</appSettings>

Custom config sections are typically used for more complicated configurations (e.g. multiple values per key, non-string values, etc.

The example is part of a larger example that is provided for the ConfigurationManager class. C# Copy. // Create the AppSettings section. // The function uses the  The ProjectBase.Data.OpenSessionInViewSection indicates that there is already a custom config section defined that will allow access to the config settings. It may, however be protected or internal to NHibernate. See if you can reference that class to access the settings.

If you want to get the value from the app settings your appsetting element in configuration file must have a key.

define your sever value as mentioned below under configuration section:

<configuration>
    <appSettings>
          <add key="server" value="http://example.com" />
    </appSettings>
    ...
    ...
    ...
</configuration>

Now execute below code line to get the server url:

string peopleXMLPath = ConfigurationManager.AppSettings["server"].ToString();

Update AppSettings and custom configuration sections in App. config at runtime. App.config file. Update AppSettings. Add a new key. Edit an existing key's value. Delete an existing key. Update custom configuration sections. Add a new key. Edit an existing key's value. Delete an existing key. Print out all keys. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to read values from custom section in web.config

Not only is the first approach prone to typos ( AppSettings["tire"] or AppSettings["​tier] anyone?) strongly typed class to represent a logical group of settings using XML Serialization. private static BlogSettings settings = ConfigurationManager. Step 3 - Enjoy your new custom configuration section {.clear}. Prefer <appSettings> to <customUserSetting> section. It is much easier to read AND write with (Web)ConfigurationManager. ConfigurationSection, ConfigurationElement and ConfigurationElementCollection require you to derive custom classes and implement custom ConfigurationProperty properties. Way too much for mere everyday mortals IMO.

Here's an example I came up with to illustrate how appSettings could be used. Let's say we want to monitor one var serverAddress = ConfigurationManager. “name” is the name of our custom configuration section: servers. You can also use appSettings as the section name in which case the standard appSettings section is used without any custom configuration section configuration. In the code above the configuration section is written automatically as part of the Initialize() code - but you can also explicitly write out configuration information using the Write

Generic; using System.Linq Configuration; using System. Specialized; using System. //one tag section IDictionary oneTag = (IDictionary)​ConfigurationManager. WriteLine(); //name value collection section (the same as appSettings) 

Comments
  • haacked.com/archive/2007/03/11/…
  • ConfigurationManager.AppSettings["MyAppSetting"] only gives you the setting keyed with the name "MyAppSetting" under <appSettings> in your configuration file.
  • Check this link stackoverflow.com/questions/6329114/…
  • using my xml does not work unfortunately,... should i change my xml?
  • Yes. You'll need to change url="http://example.com" from being an attribute on server to being a child add tag.