【发布时间】:2013-07-02 09:44:01
【问题描述】:
我想用GetPrivateProfileString 做一个INI 阅读器。我正在使用什么:
public class Config
{
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);
private string path;
public Config(string path)
{
this.path = path;
}
public string PopValue(string section, string key)
{
StringBuilder sb = new StringBuilder();
GetPrivateProfileString(section, key, "", sb, sb.Length, path);
return sb.ToString();
}
}
现在是我的 INI 文件:
[mysql]
host=localhost
以及我使用的:
Console.WriteLine(Configuration.PopValue("mysql", "host"));
但是,它只是打印出一个空行而不是localhost。我做错了什么?
【问题讨论】:
-
从不使用 GetPrivateProfileString(),由于它的 appcompat 行为,它非常昂贵。读取一个 single ini 参数需要 50 毫秒。阅读其中的 20 篇,你已经浪费了你的启动时间。格式很简单,很容易用 StreamReader 解析自己。