C# app.config file, can we set a subkey? I mean a key under another key hierarchically?

jazzIIIlove
jazzIIIlove used Ask the Experts™
on
Hi there;

In C#'s app.config, is it possible to add a subkey? I mean a key under another key hierarchically?

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="blabla" value="0"/>
           <add subkey="Root" ....is this line possible in C#. If so, how to retrieve subkey?

Best regards.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
StephanLead Software Engineer

Commented:
No this is not possible. Why do you want this? You could use XML instead of the app.config

Else you have to create your application with sections. than you can group configurations
You can put your own set of elements in root element like this
  <CustomElement>
    <FirstLevel FirstProperty="x">
      <SecondLevel SecondProperty="y"/>
    </FirstLevel>
  </CustomElement>
and make some classes to keep and get data from this elements
stephanonline:
OK.
ph_o_enix:
OK, then apply your way below: (Note that I cannot change it to a random xml, but app.config file):

E.g.
I have a class named as configurationkeys.cs
public static class ConfigurationKeys
{
          public static readonly string blabla= "blabla";
...
}

app.config is as:
<configuration>
  <appSettings>    
    <add key="blabla" value="Hello"/>
...
  </appSettings>
...

I am calling the key as:
ConfigurationManager.AppSettings[ConfigurationKeys.blabla]) which is OK, so:

1) How can I add the second level to my config file, I mean what is the keyword for such a subkey? (if exists)
2) How can I call this subkey?
Best regards.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

StephanLead Software Engineer

Commented:
You cannot create subkeys inside the appSettings.

The way ph_o_enix is proposing is creating a new element inside the root of your app.config like so:

<?xml version="1.0"?>
<configuration>
 <CustomElement>
    <FirstLevel FirstProperty="x">
      <SecondLevel SecondProperty="y"/>
    </FirstLevel>
  </CustomElement>
</configuration>

Here can you find an example for this: http://devlicio.us/blogs/derik_whittaker/archive/2006/11/13/app-config-and-custom-configuration-sections.aspx
Ok, I got the point.
I try to apply the above link, which I think very useful, but I fail:
Let me explain the app.config that I want to design:
common is common for Flag1, and Flag2 whereas if Flag1 is called, the value1 should be assigned as asdf.

<common>
<value0 =0/>
<common>

<Flag1>
<value1 = "asdf"/>
</Flag1>

<Flag2>
<value2 = "asdf"/>
</Flag2>

How can we adapt this to this:
<configuration>
  <appSettings>    
    <add key="blabla" value="Hello"/>
...
  </appSettings>
...
and how can we call those? I mean a full conf. and a call for it is what I need.

Regards.
As stephanonline said you couldn't use heirarchy in appSettings.
I could'nt understand what you need at all. please explaine more obviously.
If you want use CustomConfiguration, attachment could be usefull
this is app.config elements witch is put under root element:
  <CacheControl>
    <Caching CachingDays="90">
      <FileExtensions>
        <add Extension=".aspx" CachingHours="0" />
      </FileExtensions>
    </Caching>
  </CacheControl>
and three class to load this elements in attachment.
you can use settings by this command:
      CachingSection config = (CachingSection)HttpContext.Current.GetSection("CacheControl/Caching");

***All is Examples i use for my project - So you need to change them***
/// <summary>
  /// Configuration for caching
  /// </summary>
  public class CachingSection : ConfigurationSection
  {
    [ConfigurationProperty("CachingDays", IsRequired = true)]
    public int CachingDays
    {
      get { return (int)base["CachingDays"]; }
      set { base["CachingDays"] = value; }
    }

    [ConfigurationProperty("FileExtensions", IsDefaultCollection = true, IsRequired = true)]
    public FileExtensionCollection FileExtensions
    {
      get { return ((FileExtensionCollection)base["FileExtensions"]); }
    }
  }

  /// <summary>
  /// List of available file extensions
  /// </summary>
  public class FileExtensionCollection : ConfigurationElementCollection
  {
    public override ConfigurationElementCollectionType CollectionType
    {
      get
      {
        return ConfigurationElementCollectionType.AddRemoveClearMap;
      }
    }

    public FileExtension this[int index]
    {
      get { return (FileExtension)BaseGet(index); }
      set
      {
        if (BaseGet(index) != null)
          BaseRemoveAt(index);
        BaseAdd(index, value);
      }
    }

    public new FileExtension this[string extension]
    {
      get { return (FileExtension)BaseGet(extension); }
      set
      {
        if (BaseGet(extension) != null)
        {
          BaseRemove(extension);
        }
        BaseAdd(value);
      }
    }

    public void Add(FileExtension element)
    {
      BaseAdd(element);
    }

    public void Clear()
    {
      BaseClear();
    }

    protected override ConfigurationElement CreateNewElement()
    {
      return new FileExtension();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
      return ((FileExtension)element).Extension;
    }

    public void Remove(FileExtension element)
    {
      BaseRemove(element.Extension);
    }

    public void Remove(string name)
    {
      BaseRemove(name);
    }

    public void RemoveAt(int index)
    {
      BaseRemoveAt(index);
    }
  }

  /// <summary>
  /// Configuration for a file extension
  /// </summary>
  public class FileExtension : ConfigurationElement
  {
    [ConfigurationProperty("Extension", IsRequired = true)]
    public string Extension
    {
      get { return (string)base["Extension"]; }
      set { base["Extension"] = value; }
    }

    [ConfigurationProperty("CachingHours", IsRequired = false)]
    public int CachingHours
    {
      get { return (int)base["CachingHours"]; }
      set { base["CachingHours"] = value; }
    }

    //[ConfigurationProperty("ContentType", IsRequired = true)]
    //public string ContentType
    //{
    //  get { return (string)base["ContentType"]; }
    //  set { base["ContentType"] = value; }
    //}
  }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial