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

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.

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

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***
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
/// <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; }
    //}
  }
Random Solutions  
 
programming4us programming4us