I have created a config file within my project, and called it MyConfig.config. It contains the following:
<configuration>
<MySection MyString="StringHere"/>
</configuration>
I’’m trying to access this as follows:
AppSettingsSection settings = (AppSettingsSection)ConfigurationManager.GetSection("MySection");
string myString = settings.Settings["MyString"].Value;
Clearly I’m doing something wrong. Is it even possible to use the ConfigurationManager in this way?
Yes, it is possible. There are a couple of steps. First, you have to define your section in the config. This involves declaring the section name and the type (class you have written):
Next, you have to actually write the code to handle the section. It must inherit from IConfigurationSectionHandler:
Next, add the actual configuration item into the web.config:
Done.