根据网上资料抄了如下读写INI的一个函数 //读写INI
public class MyINI
{
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 MyINI(string INIPath)
{
inipath = INIPath;
}
// 写入INI文件
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.inipath);
}
// 读出INI文件
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, "未发现有指定值", temp, 500, this.inipath);
return temp.ToString();
}
//验证文件是否存在
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}调用如下:
MyINI nn=new MyINI(Server.MapPath(@"~/bak.ini"));
if (nn.ExistINIFile())
{
Label1.Text = nn.IniReadValue("Section1", "KeyWord2");
}
else
{
Label1.Text = "未找到文件";
}
为什么取不到值呢?
文件也存在,显示:未发现有指定值
bak.ini的内容如下
[Section1]
KeyWord1 = Valuel
KeyWord2 = Value2
public class MyINI
{
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 MyINI(string INIPath)
{
inipath = INIPath;
}
// 写入INI文件
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.inipath);
}
// 读出INI文件
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, "未发现有指定值", temp, 500, this.inipath);
return temp.ToString();
}
//验证文件是否存在
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}调用如下:
MyINI nn=new MyINI(Server.MapPath(@"~/bak.ini"));
if (nn.ExistINIFile())
{
Label1.Text = nn.IniReadValue("Section1", "KeyWord2");
}
else
{
Label1.Text = "未找到文件";
}
为什么取不到值呢?
文件也存在,显示:未发现有指定值
bak.ini的内容如下
[Section1]
KeyWord1 = Valuel
KeyWord2 = Value2
解决方案 »
- C# 正则表达式
- VB转换成C#时遇到的问题2
- c#菜鸟的问题
- 为什么我写SizeChanged事件实现系统托盘时返回DialogResult.Cancel
- 请VB高手+C#高手帮我把VB写的这个函数翻译成C#的(在线...)
- System.Net.Mail发邮件异常,求帮助
- 请教思归 将数据从库中取出后修改,再邦定到DataGrid
- winform下用Webclient上传文件,默认大小不能超过1M,该如何解决?
- 解决问题。一定给分。正哲问题。谢谢。
- 难题呀!!!
- System.ArgumentException:DataGridViewComboBoxCell值无效
- js 取不到div对象什么情况
我加了SetLastError,请问在什么地方可以看到errorcode?
{
WritePrivateProfileString(Section, Key, value, System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "SysMsg.ini");
} public string ReadInivalue(string Section, string Key)
{
StringBuilder TempStr = new StringBuilder(2000);
int i = GetPrivateProfileString(Section, Key, "", TempStr, 2000, System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "SysMsg.ini");
return TempStr.ToString();
}