我想要实现的非常简单:我有一个Windows窗体(。NET 3.5)应用程序使用路径来读取信息。用户可以使用我提供的选项表单修改此路径。
现在,我想将路径值保存到一个文件中以供以后使用。这将是保存到该文件中的众多设置之一。该文件将直接位于应用程序文件夹中。
我知道有三种选择:
配置文件(appname.exe.config)
注册表
自定义XML文件
我读到。net配置文件不能将值保存回配置文件。至于注册表,我想尽量远离它。
这是否意味着我应该使用自定义XML文件来保存配置设置?
如果是这样,我想看看代码的例子(c#)。
我看过其他关于这个问题的讨论,但我仍然不清楚。
其他选项,除了使用自定义XML文件,我们还可以使用更用户友好的文件格式:JSON或YAML文件。
如果你使用。net 4.0动态,这个库真的很容易使用
(序列化,反序列化,嵌套对象支持和排序输出
+将多个设置合并为一个)JsonConfig(使用相当于ApplicationSettingsBase)
.NET YAML配置库…我还没找到一个
易于作为JsonConfig使用
您可以将您的设置文件存储在多个特殊的文件夹中(针对所有用户和每个用户),如环境所列。SpecialFolder枚举和多个文件(默认为只读,每个角色,每个用户,等等)
获取特殊文件夹路径的示例
% AppData %
如果需要使用多个设置,可以将这些设置合并:例如“default + BasicUser + AdminUser”的设置合并。您可以使用自己的规则:最后一个规则覆盖值,等等。
registry/configurationSettings/XML参数似乎仍然非常活跃。随着技术的进步,我都用过了,但我最喜欢的是基于Threed的系统和隔离存储的结合。
下面的示例允许将名为properties的对象存储到隔离存储中的文件中。如:
AppSettings.Save(myobject, "Prop1,Prop2", "myFile.jsn");
可以使用以下方法恢复属性:
AppSettings.Load(myobject, "myFile.jsn");
这只是一个示例,并不是最佳实践的建议。
internal static class AppSettings
{
internal static void Save(object src, string targ, string fileName)
{
Dictionary<string, object> items = new Dictionary<string, object>();
Type type = src.GetType();
string[] paramList = targ.Split(new char[] { ',' });
foreach (string paramName in paramList)
items.Add(paramName, type.GetProperty(paramName.Trim()).GetValue(src, null));
try
{
// GetUserStoreForApplication doesn't work - can't identify.
// application unless published by ClickOnce or Silverlight
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForAssembly();
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write((new JavaScriptSerializer()).Serialize(items));
}
}
catch (Exception) { } // If fails - just don't use preferences
}
internal static void Load(object tar, string fileName)
{
Dictionary<string, object> items = new Dictionary<string, object>();
Type type = tar.GetType();
try
{
// GetUserStoreForApplication doesn't work - can't identify
// application unless published by ClickOnce or Silverlight
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForAssembly();
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
using (StreamReader reader = new StreamReader(stream))
{
items = (new JavaScriptSerializer()).Deserialize<Dictionary<string, object>>(reader.ReadToEnd());
}
}
catch (Exception) { return; } // If fails - just don't use preferences.
foreach (KeyValuePair<string, object> obj in items)
{
try
{
tar.GetType().GetProperty(obj.Key).SetValue(tar, obj.Value, null);
}
catch (Exception) { }
}
}
}
“这是否意味着我应该使用自定义XML文件来保存配置设置?”不,不一定。我们使用SharpConfig进行此类操作。
例如,如果配置文件是这样的
[General]
# a comment
SomeString = Hello World!
SomeInteger = 10 # an inline comment
我们可以像这样检索值
var config = Configuration.LoadFromFile("sample.cfg");
var section = config["General"];
string someString = section["SomeString"].StringValue;
int someInteger = section["SomeInteger"].IntValue;
它与。net 2.0及更高版本兼容。我们可以动态地创建配置文件,并在以后保存它。
来源:http://sharpconfig.net/
GitHub: https://github.com/cemdervis/SharpConfig
据我所知,.NET确实支持使用内置的应用程序设置功能持久化设置:
The Application Settings feature of Windows Forms makes it easy to create, store, and maintain custom application and user preferences on the client computer. With Windows Forms application settings, you can store not only application data such as database connection strings, but also user-specific data, such as user application preferences. Using Visual Studio or custom managed code, you can create new settings, read them from and write them to disk, bind them to properties on your forms, and validate settings data prior to loading and saving.
- http://msdn.microsoft.com/en-us/library/k4s6c3a0.aspx