C#.NET中的全局可编辑配置设置?

时间:2011-05-09 02:06:28

标签: c#

我想要两全其美:我希望能够在运行时保持更改,就像用户范围的应用程序设置一样,我也希望这些设置是全局的。有没有办法通过app.config设置文件来完成这个?我应该看一些其他方法来为我的应用程序保留全局,运行时可编辑设置吗?

3 个答案:

答案 0 :(得分:1)

.Net中用于处理配置文件中的应用程序设置的内置配置管理器是只读的,所以从技术上讲,你不能使用内置的库来实现,但配置文件只是xml,因此,没有理由不能使用标准xml方法更新配置文件,然后调用

ConfigurationManager.RefreshSection("appSettings")

当您想重新加载设置时

答案 1 :(得分:0)

此外,ConfigurationManager的OpenMappedExeConfiguration()方法允许您动态加载您选择的配置文件(授予它遵循.NET xml配置架构)并让您的应用程序从中加载配置选项,以便您可以对其进行修改文件@lomax表示有一个可以从所有应用程序加载的公共文件,使用相同的方法。

以下是OpenMappedExeConfiguration

的一些信息

答案 2 :(得分:0)

好的,这就是我解决它的方法:

我创建了非常基本的ConfigurationSectionConfigurationElementConfigurationElementCollection实现:

public class CoreConfigurationSection : ConfigurationSection
{
    [ConfigurationProperty("settings", IsDefaultCollection = true)]
    [ConfigurationCollection(typeof(CoreSettingCollection), AddItemName = "setting")]
    public CoreSettingCollection Settings
    {
        get
        {
            return (CoreSettingCollection)base["settings"];
        }
    }
}

public class CoreSetting : ConfigurationElement
{
    public CoreSetting() { }

    public CoreSetting(string name, string value)
    {
        Name = name;
        Value = value;
    }

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

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

public class CoreSettingCollection : ConfigurationElementCollection
{
    public new string this[string name]
    {
        get { return BaseGet(name) == null ? string.Empty : ((CoreSetting)BaseGet(name)).Value; }
        set { Remove(name); Add(name, value); }
    }

    public void Add(string name, string value)
    {
        if (!string.IsNullOrEmpty(value))
            BaseAdd(new CoreSetting(name, value));
    }

    public void Remove(string name)
    {
        if (BaseGet(name) != null)
            BaseRemove(name);
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((CoreSetting)element).Name;
    }
}

然后是一个管理配置文件的类:

public static class Settings
{
    private static string _root { get { return "core"; } }

    private static Configuration Load()
    {
        string filename = Path.Combine(Core.BaseDirectory, "core.config");

        var mapping = new ExeConfigurationFileMap {ExeConfigFilename = filename};
        var config = ConfigurationManager.OpenMappedExeConfiguration(mapping, ConfigurationUserLevel.None);

        var section = (CoreConfigurationSection)config.GetSection(_root);

        if (section == null)
        {
            Console.Write("Core: Building core.config...");

            section = new CoreConfigurationSection();
            config.Sections.Add(_root, section);
            Defaults(section);
            config.Save(ConfigurationSaveMode.Modified);

            Console.WriteLine("done");
        }

        return config;
    }

    private static void Defaults(CoreConfigurationSection section)
    {
        section.Settings["Production"] = "false";
        section.Settings["Debug"] = "false";
        section.Settings["EventBot"] = "true";
        section.Settings["WebAccounting"] = "true";
        section.Settings["AllowPlayers"] = "true";
    }

    #region Accessors

    public static string Get(string setting)
    {
        var config = Load();
        var section = (CoreConfigurationSection)config.GetSection(_root);

        return section.Settings[setting];
    }

    public static bool GetBoolean(string setting)
    {
        var config = Load();
        var section = (CoreConfigurationSection)config.GetSection(_root);

        return section.Settings[setting].ToLower() == "true";
    }

    public static void Set(string setting,string value)
    {
        var config = Load();
        var section = (CoreConfigurationSection)config.GetSection(_root);

        if (value == null)
            section.Settings.Remove(setting);

        section.Settings[setting] = value;
        config.Save(ConfigurationSaveMode.Modified);
    }

    public static void SetBoolean(string setting, bool value)
    {
        var config = Load();
        var section = (CoreConfigurationSection)config.GetSection(_root);

        section.Settings[setting] = value.ToString();
        config.Save(ConfigurationSaveMode.Modified);
    }

    #endregion

    #region Named settings

    public static bool Production
    {
        get { return GetBoolean("Production"); }
        set { SetBoolean("Production", value); }
    }

    public static bool Debug
    {
        get { return GetBoolean("Debug"); }
        set { SetBoolean("Debug", value); }
    }

    public static bool EventBot
    {
        get { return GetBoolean("EventBot"); }
        set { SetBoolean("EventBot", value); }
    }

    public static bool WebAccounting
    {
        get { return GetBoolean("WebAccounting"); }
        set { SetBoolean("WebAccounting", value); }
    }

    public static bool AllowPlayers
    {
        get { return GetBoolean("AllowPlayers"); }
        set { SetBoolean("AllowPlayers", value); }
    }

    #endregion
}

我无法想到更好的方法来制作打字配置,而不是对它们进行硬编码,但除此之外,我可以在运行时创建和更新配置,它们是全局的,可编辑的,以及位于我的应用程序根目录,所以基本上涵盖了我想要的所有功能。

如果core.config文件不存在,则会在运行时创建,只要您尝试加载或保存设置,就会对此进行验证,其中一些默认值仅为"开始使用"。 ..(你可以跳过那个"初始化"虽然。

core.config文件看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="core" type="Server.CoreConfigurationSection, ServerCore, Version=2.1.4146.38077, Culture=neutral, PublicKeyToken=null" />
    </configSections>
    <core>
        <settings>
            <setting name="Production" value="false" />
            <setting name="Debug" value="false" />
            <setting name="EventBot" value="true" />
            <setting name="WebAccounting" value="true" />
            <setting name="AllowPlayers" value="true" />
        </settings>
    </core>
</configuration>
相关问题