我想兩全其美的:我希望能夠在運行時持續變化,喜歡的用戶範圍的應用程序設置就可以了,我也希望這些設置是全球性的。有沒有辦法通過app.config設置文件來實現這一點?我應該看看其他方式來爲我的應用程序保留全局,運行時可編輯設置嗎?C#.NET中的全局可編輯配置設置?
1
A
回答
0
好了一些信息,這是我如何解決它:
我創造了真正的基本ConfigurationSection
,ConfigurationElement
和ConfigurationElementCollection
實現:
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表示和有,你可以從所有的應用程序加載,使用這種同樣的方法共同文件的文件。
相關問題
- 1. 如何設置Parsley.js全局配置
- 2. Sitecore富文本HTML編輯器配置文件 - 設置全局默認
- 3. 全局變量v C#中的設置
- 4. 如何在C#WinForms應用程序中創建可編輯的配置設置?
- 5. 將android設置存儲在可編輯配置中
- 6. 在c#中設置全局變量class
- 7. 保存asp.net用戶可編輯的配置設置
- 8. 在Objective-C中,如何創建一個全局可訪問的全局配置?
- 9. 從C++/C設置全局LUA_PATH變量?
- 10. .Net配置編輯器問題
- 11. parsley.js全局設置
- 12. 全局配置文件寫入編碼
- 13. joomla全局配置錯誤500全局配置保存
- 14. .NET 2.0的安全配置
- 15. Play Framework 2.1:在全局設置中以編程方式覆蓋配置文件
- 16. 如何在全局配置中從Oozie設置配置單元屬性
- 17. 如何在smartgit中設置git全局配置
- 18. 設置網絡配置編程C#
- 19. 可編輯的配置文件頁面
- 20. 編輯Vim配置
- 21. C#硒/ phantomjs設置全局代理
- 22. Emacs全局設置鍵到C-TAB
- 23. 配置WCF安全設置
- 24. Laravel 5設置配置文件的全局變量(mailchimp)
- 25. 爲ng-bootstrap設置工具提示的全局配置
- 26. TableView,設置可編輯的單元格
- 27. C#.NET服務參考設置從設置文件中的配置丟失
- 28. git全局配置問題
- 29. CherryPy:訪問全局配置
- 30. 全局Hadoop配置存儲