仅供自己查阅使用
public class IniConfiguration
{
public string m_FileName;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern long WritePrivateProfileString(string section, string key, string value, string FilePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern int GetPrivateProfileString(string section, string key, string Default, StringBuilder RetVal, int Size, string FilePath);
string Path;
string EXE = Assembly.GetExecutingAssembly().GetName().Name;
public string Section { set; get; }
public IniConfiguration(string iniPath)
{
Path = new FileInfo(iniPath ?? EXE + ".ini").FullName.ToString();
}
public IniConfiguration(string iniPath, string section)
{
Path = new FileInfo(iniPath ?? EXE + ".ini").FullName.ToString();
Section = section;
}
public string Read(string key, string section)
{
var RetVal = new StringBuilder(255);
GetPrivateProfileString(section ?? EXE, key, "", RetVal, 255, Path);
return RetVal.ToString();
}
public string Read(string key)
{
return Read(key, Section);
}
public void Write(string key, string value, string section)
{
WritePrivateProfileString(section ?? EXE, key, value, Path);
}
public void Write(string key, string value)
{
Write(key, value, Section);
}
public void DeleteKey(string key, string section)
{
Write(key, null, section ?? EXE);
}
public void DeleteKey(string key)
{
DeleteKey(key, Section);
}
public void DeleteSection(string section)
{
Write(null, null, section ?? EXE);
}
public void DeleteSection()
{
DeleteSection(Section);
}
public bool KeyExists(string key, string section)
{
return Read(key, section).Length > 0;
}
public bool KeyExists(string key)
{
return Read(key, Section).Length > 0;
}
}