读取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

...全文
219 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
风来我也来 2010-10-24
  • 打赏
  • 举报
回复
感谢wuyq11和civilman。
我最初没把ini文件复制到debug下面,后面调试过没错。再后面加了week后面那些,怎么就没跟着改。
回答下,就结贴了。学校熄灯了,明天早上来结贴。
风来我也来 2010-10-24
  • 打赏
  • 举报
回复
谢4楼。我去看了DEBUG目录的ini文件发现只有Main小节部分。
请问为什么会这样,我最初Myset.ini文件都和bin文件夹放在同一目录下。为什么后面MAIN跑过去了后面的其他小节没有过去?
wuyq11 2010-10-24
  • 打赏
  • 举报
回复
检查路径
public static class IniFile  {     
[DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); [DllImport("kernel32")] public static extern long WritePrivateProfileString(string section, string key, string val, string filePath); static string path = Application.StartupPath + "\\out1.ini"; //读 INI public static string GetSqlString() { if (System.IO.File.Exists(path) == false) return "";
StringBuilder temp1 = new StringBuilder(255);
int i1 = GetPrivateProfileString("Database", "server", "", temp1, 255, path);
StringBuilder strSql = new StringBuilder(255);
strSql.Append("server =" + temp1.ToString().Trim());
return strSql.ToString(); }
// 写 INI public static void WriteIni(string server, string uid, string pwd, string database) {
WritePrivateProfileString("Database", "server", server, path);
} }
civilman 2010-10-24
  • 打赏
  • 举报
回复
我试了下,完全正常啊,能读到wh 值,也没报错。
你读的是上面的ini吗?

string path = Application.StartupPath;
看下这个在哪?注意debug和release目录不同。
风来我也来 2010-10-24
  • 打赏
  • 举报
回复
查过。没有读到值,缓冲区i=0
wuyq11 2010-10-24
  • 打赏
  • 举报
回复
单步查看ini.IniReadValue("Week", "hour")值
风来我也来 2010-10-24
  • 打赏
  • 举报
回复
自己顶下。
报错是报格式错。
DEBUG了发现是没读到数据。想请教的就是为什么读不进来?(Main小节可以读进来之后就不行)

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧