读取ini文件报错:输入字符串的格式不正确。
风来我也来 2010-10-24 09:53:53 求教ini文件哪边需要修改:
报错函数:自己DUBUG一下不是字符串格式错误而是没有把值读进来。前面的y,m,d,h,mi,s都能正确读入,从wd开始都没有读入数据。
private void LoadData()
{
string path = Application.StartupPath;
Ini ini = new Ini(path + "\\MySet.ini");
y = Convert.ToInt32(ini.IniReadValue("Main", "year"));
m = Convert.ToInt32(ini.IniReadValue("Main", "month"));
d = Convert.ToInt32(ini.IniReadValue("Main", "day"));
h = Convert.ToInt32(ini.IniReadValue("Main", "hour"));
mi = Convert.ToInt32(ini.IniReadValue("Main", "minutes"));
s = Convert.ToInt32(ini.IniReadValue("Main", "second"));
wd = Convert.ToString(ini.IniReadValue("Week", "date"));
//下边报错,上面因为转化的是String没报,到下面才因为没读到数据(默认为“”)才报错。
wh = Convert.ToInt32(ini.IniReadValue("Week", "hour"));
wm = Convert.ToInt32(ini.IniReadValue("Week", "minutes"));
wsound = Convert.ToInt32(ini.IniReadValue("Week", "sound"));
dh = Convert.ToInt32(ini.IniReadValue("Day", "hour"));
dm = Convert.ToInt32(ini.IniReadValue("Day", "minutes"));
dsound = Convert.ToInt32(ini.IniReadValue("Day", "sound"));
oy = Convert.ToInt32(ini.IniReadValue("One", "year"));
om = Convert.ToInt32(ini.IniReadValue("One", "month"));
od = Convert.ToInt32(ini.IniReadValue("One", "day"));
oh = Convert.ToInt32(ini.IniReadValue("One", "hour"));
ominutes= Convert.ToInt32(ini.IniReadValue("One", "minutes"));
osound = Convert.ToInt32(ini.IniReadValue("One", "sound"));
}
调用的Ini类:
namespace 时钟控制端
{
/// <summary>
/// Ini 的摘要说明。
/// </summary>
public class Ini
{
public string path; //INI文件名
public Ini(string INIPath)
{
//
// TODO: 在此处添加构造函数逻辑
//
path = 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);
//声明读写INI文件的API函数
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
//写INI文件
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
return temp.ToString();
}
//读取INI文件指定
}
}
读取的ini文件:
[Main]
year=0
month=0
day=0
hour=0
minutes=0
second=0
[Week]
date=0
hour=0
minutes=0
second=0
sound=0
Wtrue=FALSE
[Day]
hour=0
minutes=0
sound=0
Dtrue=FALSE
[One]
year=0
month=0
day=0
hour=0
minutes=0
sound=0
Otrue=FALSE