Custom configuration file for provider configuration

I am creating my own provider and would like to know how to specify a different configuration file (for example, MyProvider.Config) so that my provider can select a configuration. The default is Web.Config.

Can I specify the path to the user configuration file in the MyProviderConfiguration class?

Example:

internal class MyProviderConfiguration : ConfigurationSection
{
    [ConfigurationProperty("providers")]        
    public ProviderSettingsCollection Providers
    {
        get
        {
            return (ProviderSettingsCollection)base["providers"];
        }
    }

    [ConfigurationProperty("default", DefaultValue = "TestProvider")]
    public string Default
    {
        get
        {
            return (string)base["default"];
        }
        set
        {
            base["default"] = value;
        }
    }
}
+3
source share
1 answer

I'm not too sure what you want to do. If you just want to download the configuration file from another place, you can do the following:

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "<config file path>";

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

MyProviderConfiguration customConfig = (MyProviderConfiguration)config.GetSection("

configSectionName ");

If you just want to put your own configuration in a separate file, you can do this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="myProviderConfiguration" type="Namespace.MyProviderConfiguration, AssemblyName" />
    </configSections>
    <myProviderConfiguration configSource="configFile.config" />
</configuration>

configFile.config :

<?xml version="1.0" encoding="utf-8"?>
<myProviderConfiguration Default="value">
    <Providers>
        <Provider />
    </Providers>
</myProviderConfiguration>

, .

0

Source: https://habr.com/ru/post/1750515/


All Articles