2

I encountered a problem that I have no way to parse a document standard configurations.

For example:

private string GetConfigKey(string param)
        {
            Configuration dllConfig = ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);
            AppSettingsSection dllConfigAppSettings = (AppSettingsSection)dllConfig.GetSection("appSettings");
            return dllConfigAppSettings.Settings[param].Value;
        }

As a result, I get the settings from the file in a form:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="host" value="mail.ololo.by"/>
  </appSettings>
  <configSections>
      <section name="provideConfig" type="System.Configuration.DictionarySectionHandler"/>
      <section name="provideMailStatus" type="System.Configuration.DictionarySectionHandler" />
  </configSections>
 <provideConfig>
     <add key="post@gate.ololo.by" value="Mail Subject 1"/>
     <add key="guga@gate.ololo.by" value="Mail Subject 2"/>
 </provideConfig>
  <provideMailStatus>
    <add key="status1" value="send"/>
    <add key="status2" value="draft"/>
    <add key="status2" value="other"/>
  </provideMailStatus>
</configuration>

but

Hashtable hashtable =
                (Hashtable)ConfigurationManager.GetSection("provideConfig");
 foreach (DictionaryEntry dictionaryEntry in hashtable)
            {
                Console.WriteLine(dictionaryEntry.Key+" "+dictionaryEntry.Value);
            }

but that's unfortunately a configSections have a problem. I can not seem to get it. MB, I go in the wrong direction?

P.S. Config file cannot be named "app.config" - only project dll name

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
Y K
  • 408
  • 1
  • 5
  • 21

2 Answers2

2

Config file should be named as executable file name and ".config". Even this executable file uses this dll-file. For example: ConSoleApplication.exe uses MyLibrary.dll. Then config file must be named ConSoleApplication.exe.config

If You need some other name for config file read this

Community
  • 1
  • 1
Roman Melnyk
  • 1,097
  • 1
  • 8
  • 21
  • I have same problem in block "section" into part are getting some configuration into file. I cannot parse ".config" file and take struct my section in file. I cannot get Dictionary keys and value. That is my general problems. Config file name for me, i think, no special role played – Y K Oct 04 '12 at 08:58
  • If You haven't problem with access to file. You could use XmlSerializer for deserialize this file. But in this case i sugest to You store and in separate xml file. – Roman Melnyk Oct 04 '12 at 10:04
  • WHY? I haven't any problem with access file. I have same problem with getting section. – Y K Oct 04 '12 at 10:26
  • I think, I must create "Class" are inherited by ConfigurationSection, isn't it? – Y K Oct 04 '12 at 11:21
  • 1
    I guess You need [this](http://msdn.microsoft.com/en-us/library/2tw134k3%28v=vs.100%29.aspx) – Roman Melnyk Oct 04 '12 at 13:16
  • yep, that was my problem, but I understood and made differently. I Parsed as XML document. LInk url i cannot insert , because I forgot where I found it. Thank you all. – Y K Oct 09 '12 at 09:58
2

Thank you all, I coped with the problem. I can tell if someone will be interested.

Configuration Section Designer Codeplex

.NET Configuration Code Generator

Best regards, yauhen.

Y K
  • 408
  • 1
  • 5
  • 21