C#读取INI文件问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 16:05:29
public string inipath;
[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 INIClass(string INIPath)
{
inipath = INIPath;
}
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, inipath);
}
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "读取错误", temp, 255, inipath);
return temp.ToString();
}
调用IniReadValue方法读取不到数据,读到的是"读取错误",是怎么回事

http://topic.csdn.net/u/20080421/16/a46a4aaf-1d89-4df6-90c6-5b0380c0e705.html

确定你的INI文件可以被访问到吗?如果程序确实找到了INI文件的话,那就是你这个INI文件对应的Section和Key下面是空的(没有值)。请为你的INI文件加入相应的Section和Key再试试看。

GetPrivateProfileString(Section, Key, "读取错误", temp, 255, inipath); 表示如果读取INI文件失败(为空)就返回"读取错误"。

http://heisetoufa.javaeye.com/blog/295337