用于设置一个简单的格式是好老*.INI format。它看起来像这样:
[SECTION1]
设定1 =值1
设定2 =值2
[另一部分]
Setting3 =值3
魅力=高
强度= 2
虽然XML或JSON等其他格式更通用且允许构建co复杂的层次结构,即使是非程序员,INI文件也很容易被大家理解。
你可以写一个类的这些设置这样
public class IniSettings
{
private string _filename;
public IniSettings(string filename)
{
_filename = filename;
}
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key,
string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key,
string def, StringBuilder retVal, int size, string filePath);
public string this[string section, string key]
{
get
{
StringBuilder sb = new StringBuilder(1024);
GetPrivateProfileString(section, key, String.Empty, sb, 1024, _filename);
return sb.ToString();
}
set
{
WritePrivateProfileString(section, key, value, _filename);
}
}
}
访问使用方法如下:
var settings = new IniSettings(myIniFile);
string charisma = settings["Another section", "Charisma"];
我会推荐你研究XML或JSON格式的文件是否适合你的需求。 –