Copy codeThe Code is as follows: using System;
Using System. Runtime. InteropServices;
Using System. Text;
Using System. IO;
Namespace Common
{
/// <Summary>
/// INI file read/write class.
/// </Summary>
Public class INIFile
{
Public string path;
Public INIFile (string INIPath)
{
Path = 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 );
[DllImport ("kernel32")]
Private static extern int GetPrivateProfileString (string section, string key, string defVal, Byte [] retVal, int size, string filePath );
/// <Summary>
/// Write the INI File
/// </Summary>
/// <Param name = "Section"> </param>
/// <Param name = "Key"> </param>
/// <Param name = "Value"> </param>
Public void IniWriteValue (string Section, string Key, string Value)
{
WritePrivateProfileString (Section, Key, Value, this. path );
}
/// <Summary>
/// Read the INI File
/// </Summary>
/// <Param name = "Section"> </param>
/// <Param name = "Key"> </param>
/// <Returns> </returns>
Public string IniReadValue (string Section, string Key)
{
StringBuilder temp = new StringBuilder (255 );
Int I = GetPrivateProfileString (Section, Key, "", temp, 255, this. path );
Return temp. ToString ();
}
Public byte [] IniReadValues (string section, string key)
{
Byte [] temp = new byte [1, 255];
Int I = GetPrivateProfileString (section, key, "", temp, 255, this. path );
Return temp;
}
/// <Summary>
/// Delete all paragraphs in the INI File
/// </Summary>
Public void ClearAllSection ()
{
IniWriteValue (null, null, null );
}
/// <Summary>
/// Delete all keys in the personal section of the INI File
/// </Summary>
/// <Param name = "Section"> </param>
Public void ClearSection (string Section)
{
IniWriteValue (Section, null, null );
}
}
}
There is not much content, but it is still useful for small skills.