Provides a store for user settings in the same format as appSettings
Object Model
Syntax
Example
Library/Library.Test/TestUserSettings.cs
C# | Copy Code |
---|
UserSettingsSection settings = UserSettingsSection.DefaultSettings;
Assert.IsNull(settings);
Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
Assert.IsNull(cfg.GetSection("userSettings"));
settings = UserSettingsSection.UserSettingsFrom(cfg);
Assert.IsNotNull(settings);
Assert.IsNotNull(settings.Settings);
Assert.IsNotNull(settings.Sections);
settings.Settings.Add("a", "b");
settings["1"] = "2";
Assert.AreEqual("2", settings["1"]);
settings["1"] = "3";
Assert.AreEqual("3", settings["1"]);
cfg.Save();
ConfigurationManager.RefreshSection("userSettings");
Assert.IsNotNull(UserSettingsSection.DefaultSettings);
Assert.AreEqual("b", UserSettingsSection.DefaultSettings["a"]);
Assert.IsNotNull(UserSettingsSection.UserSettings);
Assert.AreEqual("b", UserSettingsSection.UserSettings["a"]);
UserSettingsSection copy = new UserSettingsSection();
settings["a"] = "b";
settings["1"] = "2";
settings.Sections.Add("child.test").Settings.Add("AA", "BB");
copy.CopyFrom(settings);
Assert.AreEqual("b", settings["a"]);
Assert.AreEqual("2", settings["1"]);
Assert.AreEqual("BB", settings.Sections["child.test"]["AA"]); |
VB.NET | Copy Code |
---|
Dim settings As UserSettingsSection = UserSettingsSection.DefaultSettings
Assert.IsNull(settings)
Dim cfg As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal)
Assert.IsNull(cfg.GetSection("userSettings"))
settings = UserSettingsSection.UserSettingsFrom(cfg)
Assert.IsNotNull(settings)
Assert.IsNotNull(settings.Settings)
Assert.IsNotNull(settings.Sections)
settings.Settings.Add("a", "b")
settings("1") = "2"
Assert.AreEqual("2", settings("1"))
settings("1") = "3"
Assert.AreEqual("3", settings("1"))
cfg.Save()
ConfigurationManager.RefreshSection("userSettings")
Assert.IsNotNull(UserSettingsSection.DefaultSettings)
Assert.AreEqual("b", UserSettingsSection.DefaultSettings("a"))
Assert.IsNotNull(UserSettingsSection.UserSettings)
Assert.AreEqual("b", UserSettingsSection.UserSettings("a"))
Dim copy As New UserSettingsSection()
settings("a") = "b"
settings("1") = "2"
settings.Sections.Add("child.test").Settings.Add("AA", "BB")
copy.CopyFrom(settings)
Assert.AreEqual("b", settings("a"))
Assert.AreEqual("2", settings("1"))
Assert.AreEqual("BB", settings.Sections("child.test")("AA")) |
Inheritance Hierarchy
Requirements
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7
See Also