Cannot convert type 'System.Configuration.ConfigurationSection' to 'System.Collections.Specialized.NameValueCollection'
Good evening!
I've tried casting the config object to NameValueCollection using .NET Framework 4.5.2 of Visual Studio 2015 which I read from a tutorial on how to read config files using type NameValueSectionHandler. However, as I use the code below to cast the ConfigurationSection object to NameValueCollection,
C# Code
I get an error which is the title of this post. It seems that the code was applicable to older versions of .NET Framework and not the recent ones.
After doing some research, I found out that the workaround is to use ConfigurationManager.GetSection() in which the parameter passed is the SectionName of the config object instead of directly casting the ConfigurationSection object returned by the config.GetSection() method.
C# Code
Output
That's it!
I've tried casting the config object to NameValueCollection using .NET Framework 4.5.2 of Visual Studio 2015 which I read from a tutorial on how to read config files using type NameValueSectionHandler. However, as I use the code below to cast the ConfigurationSection object to NameValueCollection,
C# Code
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
NameValueCollection scriptFiles = (NameValueCollection)config.GetSection("scriptfiles");
After doing some research, I found out that the workaround is to use ConfigurationManager.GetSection() in which the parameter passed is the SectionName of the config object instead of directly casting the ConfigurationSection object returned by the config.GetSection() method.
C# Code
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
NameValueCollection scriptFiles = (NameValueCollection)ConfigurationManager.GetSection(config.GetSection("scriptfiles").SectionInformation.SectionName);
That's it!
Thank you so much!!!!
ReplyDeleteYou're welcome
Delete