INI is a file with the extension "ini". This file mainly stores users' selections and various system parameters. You can modify the INI file to change many configurations of applications and systems.
C # The namespace does not directly read and write ini classes. Of course, if you use the int type as a text file and use the system. Io class to read and write data, you can directly use break.
In the "kernel32.dll" file, Win32 API functions-writeprivateprofilestring () and getprivateprofilestring () can be used to read and write *. ini files.
Declare the write operation function writeprivateprofilestring ():
[Dllimport ("Kernel32")]
/// Section: Section in the INI File
/// Key: keyword in the INI File
/// VAL: value of the keyword in the INI File
/// Filepath: the complete path and name of the INI File
Private Static extern long writeprivateprofilestring (string section, string key, string Val, string filepath)
Declare the read operation function getprivateprofilestring ():
[Dllimport ("Kernel32")]
/// Section: section name in the INI File
/// Key: keyword in the INI File
/// Def: default value when cannot be read
/// Retval: Read the value
/// Size: value size
/// Filepath: the complete path and name of the INI File
Private Static extern int getprivateprofilestring (string section, string key, string def, stringbuilder retval, int size, string filepath)
The operation class of *. ini file is as follows:
Public class iniclass
{
Public 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 );
/// <Summary>
/// </Summary>
/// <Param name = "inipath"> file path </param>
Public iniclass (string inipath)
{
Inipath = inipath;
}
/// <Summary>
/// Write the INI File
/// </Summary>
/// <Param name = "section"> paragraph name </param>
/// <Param name = "key"> keyword </param>
/// <Param name = "value"> value of the keyword </param>
Public void iniwritevalue (string section, string key, string value)
{
Writeprivateprofilestring (section, key, value, this. inipath );
}
/// <Summary>
/// Read the INI File
/// </Summary>
/// <Param name = "section"> paragraph name </param>
/// <Param name = "key"> keyword </param>
Public String inireadvalue (string section, string key)
{
Stringbuilder temp = new stringbuilder (500 );
Int I = getprivateprofilestring (section, key, "", temp, 500, this. inipath );
Return temp. tostring ();
}
/// <Summary>
/// Verify whether the file exists
/// </Summary>
Public bool existinifile ()
{
Return file. exists (inipath );
}
}