将自定义部分保存到配置文件 [英] Saving custom section to config file

查看:31
本文介绍了将自定义部分保存到配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,基本上我想在我的配置文件中实现以下结果:

I have a small issue, basically I would like to achieve the following result in my config file:

<customSection>
<settings>
<setting key="" value="" />
<setting key="" value=""/>
...
</settings>
</customSection>

我已经创建了以下代码,但在第一次运行时,我无法使用我传递的值创建结构!

I've created the following code, but on the first run i cannot have it created the structure with the values that i pass!

这是初始化此配置代码并使用值构建默认结构的代码:

Here is the code that initializes this configuration code and builds the default structure with values:

ConfigurationSectionManager config = new ConfigurationSectionManager("CustomSection");
config.CreateDefaultConfigurationSection("CustomSection");
Log("Get value  = " + (config.GetSection() as ConfigurationSectionManager).Settings[1].Value); //instead of [1] a key should be set ...

现在是配置节管理器的代码:

Now the code for the configuration section manager:

public class ConfigurationSectionManager: ConfigurationSection
{
    private const string defaultSectionName = "Default";

    private string sectionName;
    public string SectionName 
    {
        get 
        {
            if (string.IsNullOrEmpty(sectionName))
            {
                return defaultSectionName;
            }
            return sectionName;
        }
        set
        {
            sectionName = value;
        }
    }

    public SancoConfigurationSectionManager(string sectionName)
    {
        SectionName = sectionName;
    }

    public void CreateDefaultConfigurationSection(string sectionName)
    {
        ConfigurationSectionManager defaultSection = new ConfigurationSectionManager(sectionName);
        SettingsConfigurationCollection settingsCollection = new SettingsConfigurationCollection();
        settingsCollection[0] = new SettingConfigurationElement() { Key="Element", Value="Element value" };
        settingsCollection[1] = new SettingConfigurationElement() { Key = "NewElement", Value = "NeValueElement" };
        settingsCollection[2] = new SettingConfigurationElement() { Key = "NewElement2", Value = "NeValueElement2" };
        defaultSection.Settings = settingsCollection;
        CreateConfigurationSection(sectionName, defaultSection);
    }

    public void CreateConfigurationSection(string sectionName, ConfigurationSection section)
    {
        var config = ConfigurationManager.OpenExeConfiguration(null);
        if (config.Sections[SectionName] == null)
        {
            config.Sections.Add(SectionName, section);
            section.SectionInformation.ForceSave = true;
            config.Save(ConfigurationSaveMode.Full);
        }
    }

    public object GetSection()
    {
        return ConfigurationManager.GetSection(SectionName);
    }

    public override bool IsReadOnly()
    {
        return false;
    }

    public void Save()
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(null);
        ConfigurationSectionManager instance = (ConfigurationSectionManager)config.Sections[SectionName];
        instance.Settings = this.Settings;
        config.Save(ConfigurationSaveMode.Full);
    }

    [ConfigurationProperty("Settings", IsRequired = false)]
    public SettingsConfigurationCollection Settings 
    { 
        get { return this["Settings"] as SettingsConfigurationCollection; }
        set { this["Settings"] = value; }
    }

}

现在是设置集合的代码

public class SettingsConfigurationCollection : ConfigurationElementCollection
{
    public SettingConfigurationElement this[int index] 
    { 
        get 
        { 
            return BaseGet(index) as SettingConfigurationElement; 
        } 
        set 
        { 
            if (Count > index && BaseGet(index) != null) 
            { 
                BaseRemoveAt(index); 
            } 
            BaseAdd(index, value); 
        } 
    }     

    protected override ConfigurationElement CreateNewElement() 
    { 
        return new SettingConfigurationElement(); 
    }      

    protected override object GetElementKey(ConfigurationElement element) 
    { 
        return ((SettingConfigurationElement)element).Key; 
    }
}

以及设置元素的代码

public class SettingConfigurationElement : ConfigurationElement
{
    [ConfigurationProperty("key", IsRequired = true)]
    public string Key 
    { 
        get { return this["key"] as string; }
        set { this["key"] = value; }
    }

    [ConfigurationProperty("value", IsRequired = true)]
    public string Value 
    { 
        get { return this["value"] as string; }
        set { this["value"] = value; }
    }

    public override bool IsReadOnly()
    {
        return false;
    }

}

当我尝试执行所有这些操作时,出现错误:

When i try to do all of this i get an error:

Unable to load type 'MyApp.Utilities.ConfigurationSectionManager, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd112ea1ee9f48f2' because it is not public.    at System.Configuration.TypeUtil.GetConstructorWithReflectionPermission(Type type, Type baseType, Boolean throwOnError)
   at System.Configuration.MgmtConfigurationRecord.AddConfigurationSection(String group, String name, ConfigurationSection configSection)
   at System.Configuration.ConfigurationSectionCollection.Add(String name, ConfigurationSection section)

所以基本上我不能创建设置等...

So basically i cannot create settings etc ...

有人知道如何使所有这些工作吗?

Anyone has any idea how to make all of this work?

推荐答案

您的 ConfigurationSectionManager 缺少默认构造函数.添加并执行.

Your ConfigurationSectionManager is missing the default constructor. Add it and execute.

这篇关于将自定义部分保存到配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆