C# 中是否有允许您读取和写入配置或某种配置文件的库?
我不是在谈论 app.config,而是更像是一个类似于 ol .ini 文件的 custom.config,其中内置了 lib/functions 以读取/写入配置文件。
我当然可以自己写,但想知道是否存在一些通用的库或方法...
我知道自定义配置部分,但不想走那条路。
解决方案
我找到了我要找的东西。这将起作用:
http://www.codeproject.com/Articles/14465/Specify-a-Configuration-File-at-Runtime-for-a-C-Co
最佳答案
我会将自定义配置作为一个类
,然后使用XmlSerializer
来读取和写入配置值。
public class MyConfig
{
const string configPath = @"...";
public string Setting1 { get; set; }
// ...
public static MyConfig Load()
{
var serializer = new XmlSerializer(typeof(MyConfig));
using (var reader = new StreamReader(configPath))
return (MyConfig)serializer.Deserialize(reader);
}
public void Save()
{
var serializer = new XmlSerializer(typeof(MyConfig));
using (var writer = new StreamWriter(configPath))
serializer.Serialize(writer, this);
}
}
关于c# - 在 C# 中读取和写入自定义配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14410370/