写一个操作 .ini文件的类

时间:2023-01-03 21:23:01

class IniHelp
{
private 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 IniHelp(string iniPath)
{
this.iniPath = iniPath;
}
public IniHelp()
{

}
public void setFilePath(string filePath)
{
this.iniPath = filePath;

}
public void iniWrite(string section,string key,string val)
{
WritePrivateProfileString(section,key,val,iniPath);
}
public string iniReadValue(string section,string key)
{
StringBuilder temp=new StringBuilder(500);
int i=GetPrivateProfileString(section,key,"",temp,500,iniPath);
return temp.ToString();
}
public bool existIniFile()
{
return File.Exists(iniPath);
}

}