/* iniOperator.cs */
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace AutoLogin
{
/// <summary>
/// iniOperator 的摘要说明。
/// </summary>
// TODO: 在此处添加构造函数逻辑
public class iniOperator
{
public string inipath;
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
/// <summary>
/// 构造方法
/// </summary>
/// <param name="INIPath">文件路径</param>
public iniOperator(string INIPath)
{
inipath = INIPath;
}
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">项目名称(如 [TypeName] )</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.inipath);
}
/// <summary>
/// 读出INI文件
/// </summary>
/// <param name="Section">项目名称(如 [TypeName] )</param>
/// <param name="Key">键</param>
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(1024);
int i = GetPrivateProfileString(Section,Key,"",temp,1024,this.inipath);
return temp.ToString();
}
/// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值</returns>
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}
}
/* form.cs 读取INI文件部分 */
iniOperator _iniOp = new iniOperator("Messages.ini"); //文件放在Debug目录下面
if (_iniOp.ExistINIFile() == false)
{ MessageBox.Show("未找到ini文件,请检查路径!");
}
else
{ while (n % 2 == 1)
{ for (i = 0; i <= 10; i++)
{
txtAnswer.value = _iniOp.IniReadValue("No"+ i, "content");
}
}
}/* Messages.ini文件 */[No0]
content=呵呵,大家现在似乎都不活跃了。加油啊,一起来盖啊。
[No1]
content=好象就我现在没事。。是不是都忙去了
[No2]
content=好慢啊,就我一个人啊失望
[No3]
content=下午上网的人好象不多呢
[No4]
content=领导不在,好象就我这么闲呵呵
[No5]
content=斑竹别说我恶意灌水去就行。
[No6]
content=我是给大家打地基呢呵呵。
[No7]
content=大家加油啊,我昨天顶了半天还是给别人当炮灰了
[No8]
content=再发一会就不发了,。无聊了
[No9]
content=时间好慢啊,大家都不来啊,就我一个在这里顶人多力量大啊
[No10]
content=强烈建议楼主把总高设为500啊呵呵目前的问题就是文件存在,但是单步调试完了之后temp里面是空值。好像GetPrivateProfileString这个函数没起作用似的,有哪位高人指点一二,十分感谢哈!能解决问题50分答谢哈
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace AutoLogin
{
/// <summary>
/// iniOperator 的摘要说明。
/// </summary>
// TODO: 在此处添加构造函数逻辑
public class iniOperator
{
public string inipath;
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
/// <summary>
/// 构造方法
/// </summary>
/// <param name="INIPath">文件路径</param>
public iniOperator(string INIPath)
{
inipath = INIPath;
}
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">项目名称(如 [TypeName] )</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.inipath);
}
/// <summary>
/// 读出INI文件
/// </summary>
/// <param name="Section">项目名称(如 [TypeName] )</param>
/// <param name="Key">键</param>
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(1024);
int i = GetPrivateProfileString(Section,Key,"",temp,1024,this.inipath);
return temp.ToString();
}
/// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值</returns>
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}
}
/* form.cs 读取INI文件部分 */
iniOperator _iniOp = new iniOperator("Messages.ini"); //文件放在Debug目录下面
if (_iniOp.ExistINIFile() == false)
{ MessageBox.Show("未找到ini文件,请检查路径!");
}
else
{ while (n % 2 == 1)
{ for (i = 0; i <= 10; i++)
{
txtAnswer.value = _iniOp.IniReadValue("No"+ i, "content");
}
}
}/* Messages.ini文件 */[No0]
content=呵呵,大家现在似乎都不活跃了。加油啊,一起来盖啊。
[No1]
content=好象就我现在没事。。是不是都忙去了
[No2]
content=好慢啊,就我一个人啊失望
[No3]
content=下午上网的人好象不多呢
[No4]
content=领导不在,好象就我这么闲呵呵
[No5]
content=斑竹别说我恶意灌水去就行。
[No6]
content=我是给大家打地基呢呵呵。
[No7]
content=大家加油啊,我昨天顶了半天还是给别人当炮灰了
[No8]
content=再发一会就不发了,。无聊了
[No9]
content=时间好慢啊,大家都不来啊,就我一个在这里顶人多力量大啊
[No10]
content=强烈建议楼主把总高设为500啊呵呵目前的问题就是文件存在,但是单步调试完了之后temp里面是空值。好像GetPrivateProfileString这个函数没起作用似的,有哪位高人指点一二,十分感谢哈!能解决问题50分答谢哈
GetPrivateProfileString和WritePrivateProfileString
使用的是全路径.
iniOperator _iniOp = new iniOperator(Application.StartupPath+"\\Messages.ini"); //文件放在Debug目录下面