INI is a file with the extension "INI". In fact, INI is a text file. It can work in notepad and mainly stores user choices or various system parameters.
The INI file is not a common text file. it has its own structure. it consists of several sections. Under each title with parentheses, It is a number of keywords that begin with a single word (KEYWORD) and an equal sign, the right side of the equal sign is the VALUE of the keyword ). for example:
[Section1]
KeyWord1 = Value1
KeyWord2 = Value2
...
[Section2]
KeyWord3 = Value3
KeyWord4 = Value4
C # The namespace does not directly read and write INI classes. Of course, if you use INT as a text file and use the System. IO class to read and write data, I didn't say it.
I am introducing the system's INI processing method.
Although C # does not exist, there are Win32 API functions in the "kernel32.dll" file -- WritePrivateProfileString () and GetPrivateProfileString ()
C # declare the write operation function WritePrivateProfileString () of the INI file ():
[DllImport ("kernel32")]
Private static extern long WritePrivateProfileString (string section, string key, string val
, String filePath );
Parameter description: section in the INI file; key: keyword in the INI file; val: value of the keyword in the INI file; filePath: complete path and name of the INI file.
C # declare the INI file's read operation function GetPrivateProfileString ():
[DllImport ("kernel32")]
Private static extern int GetPrivateProfileString (string section,
String key, string def, StringBuilder retVal,
Int size, string filePath );
Parameter description: section name in the INI file; key: keyword in the INI file; def: default value when the file cannot be read; retVal: read value; size: value size; filePath: the complete path and name of the INI file.
The following is a class for reading and writing INI files:
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>
/// Constructor
/// </Summary>
/// <Param name = "INIPath"> file path </param>
Public INIClass (string INIPath)
{
Inipath = INIPath;
}