我正在努力實現在我的.NET C#應用程序中保存用戶設置,並且在一種情況下我只想保存一個設置。是否有可能做到這一點,或者是在一次保存所有用戶設置的標準我唯一的選擇:在C中保存單個用戶設置#
Properties.Settings.Default.Save();
我正在努力實現在我的.NET C#應用程序中保存用戶設置,並且在一種情況下我只想保存一個設置。是否有可能做到這一點,或者是在一次保存所有用戶設置的標準我唯一的選擇:在C中保存單個用戶設置#
Properties.Settings.Default.Save();
我更喜歡使用NINI,並存儲在某個地方XML配置文件中像Environment.SpecialFolder.ApplicationData
。
可能不像.NET設置那麼簡單。無論如何,我從未真正使用過它們。
例如,我有以下課程。所有我需要做的是獲取或設置屬性,它們會自動加載/保存:
using Nini.Config;
public class DbConfig : PropertyNotifierBase {
private static readonly string PROGRAM_NAME = "programname";
private static readonly string CONFIG_NAME = "Database";
private static DbConfig _instance = new DbConfig();
public static DbConfig Instance { get { return (_instance); } }
private DbConfig() {
SetupPaths();
Source = new XmlConfigSource(FullConfigFilename);
Source.AutoSave = true;
CreateSectionsIfNeeded();
}
private void CreateSectionsIfNeeded() {
if (Source.Configs["Database"] == null)
Source.AddConfig("Database");
}
private void SetupPaths() {
ConfigPath = DetermineConfigPath();
ConfigFilename = String.Format("{0}.xml", CONFIG_NAME);
Directory.CreateDirectory(ConfigPath);
// Create an empty configuration file if it isn't there.
if (!File.Exists(FullConfigFilename))
File.WriteAllText(FullConfigFilename, "<Nini>\n</Nini>\n");
}
private IConfigSource Source { get; set; }
public String ConfigPath { get; private set; }
public String ConfigFilename { get; private set; }
public String FullConfigFilename { get { return (Path.Combine(ConfigPath, ConfigFilename)); } }
public String SqlServerInstance {
get { return (Source.Configs["Database"].GetString("SqlServerInstance", @"somedefaultconnection")); }
set { Source.Configs["Database"].Set("SqlServerInstance", value); NotifyPropertyChanged("SqlServerInstance"); }
}
public String SqlServerDatabase {
get { return (Source.Configs["Database"].GetString("SqlServerDatabase", "somedatabasename")); }
set { Source.Configs["Database"].Set("SqlServerDatabase", value); NotifyPropertyChanged("SqlServerDatabase"); }
}
public String SqlServerUsername {
get { return (Source.Configs["Database"].GetString("SqlServerUsername", "someusername")); }
set { Source.Configs["Database"].Set("SqlServerUsername", value); NotifyPropertyChanged("SqlServerUsername"); }
}
public String SqlServerPassword {
get { return (Source.Configs["Database"].GetString("SqlServerPassword", "somepassword")); }
set { Source.Configs["Database"].Set("SqlServerPassword", value); NotifyPropertyChanged("SqlServerPassword"); }
}
private string DetermineConfigPath() {
String filename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
filename += Path.DirectorySeparatorChar + PROGRAM_NAME;
return (filename);
}
}
你可以有目標用戶編輯是一個不同的人比'Properties.Settings.Default',和在調用'Save()' – millimoose 2013-04-07 23:06:36