// 声明INI文件的写操作函数 WritePrivateProfileString()
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);//section=配置节,key=键名,value=键值,path=路径
// 声明INI文件的读操作函数 GetPrivateProfileString()
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);//section=配置节,key=键名,def=无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。
private string sPath = null;
public RdWriteINI(string path)
{
this.sPath = path;
}
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="section">配置节</param>
/// <param name="key">键名</param>
/// <param name="value">键值</param>
public void Write(string section, string key, string value)
{
// section=配置节,key=键名,value=键值,path=路径
WritePrivateProfileString(section, key, value, sPath);
}
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="section">配置名</param>
/// <param name="key">键名</param>
/// <returns>键值</returns>
public string ReadValue(string section, string key)
{
// 每次从ini中读取多少字节
System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
// section=配置节,key=键名,temp=上面,path=路径
GetPrivateProfileString(section, key, "", temp, 255, sPath);
return temp.ToString();
}
/// <summary>
/// 读INI文件获取URL路径
/// </summary>
/// <returns>返回List字符串</returns>
public List<string> keyName( )
{
string[] strTem = File.ReadAllLines(sPath);
List<string> lsStr = new List<string>();
for(int i=1;i<strTem .Length;i++)
{
lsStr.Add(strTem[i].Substring(0,4));//截取每一户的前4个字符
}
return lsStr;
}
}
INI文件的读写
最新推荐文章于 2017-07-10 09:44:00 发布