2011-05-09 46 views
1

我想兩全其美的:我希望能夠在運行時持續變化,喜歡的用戶範圍的應用程序設置就可以了,我也希望這些設置是全球性的。有沒有辦法通過app.config設置文件來實現這一點?我應該看看其他方式來爲我的應用程序保留全局,運行時可編輯設置嗎?C#.NET中的全局可編輯配置設置?

回答

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> 
1

內置在.NET配置管理器,它被用來在配置文件處理應用程序設置爲只讀,所以理論上講,你不能用它做內置在圖書館,然而,該配置文件只是XML ,所以沒有理由你不能使用標準的XML方法,只需更新配置文件,然後調用

ConfigurationManager.RefreshSection("appSettings") 

當你要重新加載設置

0

此外,OpenMappedExeConfiguration()的方法ConfigurationManager允許您動態加載您選擇的配置文件(授予它遵循.NET xml配置模式)和你的應用程序負載configuariton選擇從它,這樣你就可以modifcations爲@lomax表示有,你可以從所有的應用程序加載,使用這種同樣的方法共同文件的文件。

這裏有OpenMappedExeConfiguration