App.config:

<configuration> <configSections> <section name = "PokerClients" type = "WindowsFormsApplication1.PokerClientsConfig"/> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <PokerClients> <PokerClient key="PokerStars" name ="Poker stars" wndclass = "PokerStarsTableFrameClass"/> <PokerClient key="poker" name ="888 Poker" wndclass = "Poker888StarsTableFrameClass"/> </PokerClients> </appSettings> </configuration> 

here is the implementation (taken from here ).

 namespace WindowsFormsApplication1 //"модуль формы" { public class PokerClient : ConfigurationElement { [ConfigurationProperty("key", IsRequired = true)] public string Key { get { return this["key"] as string; } } [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return this["name"] as string; } } [ConfigurationProperty("wndclass", IsRequired = true)] public string Wndclass { get { return this["wndclass"] as string; } } } public class PokerClients : ConfigurationElementCollection { public PokerClient this[int index] { get { return base.BaseGet(index) as PokerClient; } set { if (base.BaseGet(index) != null) { base.BaseRemoveAt(index); } this.BaseAdd(index, value); } } public new PokerClient this[string responseString] { get { return (PokerClient)BaseGet(responseString); } set { if (BaseGet(responseString) != null) { BaseRemoveAt(BaseIndexOf(BaseGet(responseString))); } BaseAdd(value); } } protected override System.Configuration.ConfigurationElement CreateNewElement() { return new PokerClient(); } protected override object GetElementKey(System.Configuration.ConfigurationElement element) { return ((PokerClient)element).Name; } } public class PokerClientsConfig : ConfigurationSection { public static PokerClientsConfig GetConfig() { return (PokerClientsConfig)ConfigurationManager.GetSection("PokerClients") ?? new PokerClientsConfig(); } [System.Configuration.ConfigurationProperty("PokerClients")] [ConfigurationCollection(typeof(PokerClients), AddItemName = "PokerClient")] public PokerClients PokerClients { get { object o = this["PokerClients"]; return o as PokerClients; } } } 

}


when trying to get a section (method GetConfig)

 var Config = PokerClientsConfig.GetConfig(); 

an error occurs:

Raw exception of type "System.Configuration.ConfigurationErrorsException" in System.Configuration.dll

Additional information: Error while creating a configuration section handler for PokerClients: Failed to load type "WindowsFormsApplication1.PokerClientsConfig" from the assembly "System.Configuration, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a"

What do I need to write in the type property? I understood that [namespace] is being written there. [Class handler] and then in most cases the same namespace (I tried it too), but also “I cannot load a file or assembly”.

  • Does the PokerClients section in the configuration file have to be inside appSettings? You described it as an independent section. - qzavyer
  • Yes, maybe this is true. But this does not solve the problem - the framework cannot load the type "WindowsFormsApplication1.PokerClientsConfig" or does not see it. What do I need to specify? - Sergey Ser

2 answers 2

You should specify the assembly-qualified type name so that the configuration system can find it among all the assemblies:

  <configSections> <section name = "PokerClientSection" type = "WindowsFormsApplication1.PokerClientsConfig, WindowsFormsApplication1"/> </configSections> 

Here I assume that the name of your assembly matches the name of the namespace, i.e. The assembly should be in the WindowsFormsApplication1.exe file or WindowsFormsApplication1.dll . If not, enter the desired name.

  • I tried to specify: PokerForcesApplication1 'or one of its dependencies. Cannot find the file specified. - Sergey Ser
  • @ SergeySer and how is your assembly called? - Pavel Mayorov
  • Yes, it turned out. The name of my assembly does not match the namespace name in which the handler class is located. I incorrectly thought that the "assembly" and the namespace are the same. - Sergey Ser

You have the section name and the collection class name is the same ( PokerClients ). Try changing the configuration file as follows:

 <configuration> <configSections> <section name = "PokerClientSection" type = "WindowsFormsApplication1.PokerClientsConfig"/> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings></appSettings> <PokerClientSection> <PokerClients> <PokerClient key="PokerStars" name ="Poker stars" wndclass = "PokerStarsTableFrameClass"/> <PokerClient key="poker" name ="888 Poker" wndclass = "Poker888StarsTableFrameClass"/> </PokerClients> </PokerClientSection> </configuration> 

should work

  • I changed the file as you said, I try even this way var section = ConfigurationManager.GetSection ("PokerClientSection"); pops up ConfigurationErrorException: PokerClientSection: Could not load type 'WindowsFormsApplication1.PokerClientsConfig' from assembly - Sergey Ser