C # Read and Write. ini files
C # to read and write the. ini file in the code, you need to import the GetPrivateProfileString and WritePrivateProfileString functions in kernel32.dll.
First, introduce kernel32.dll.
Kernel32.dll is an important 32-bit dynamic link library file in Windows9x/Me, and is a kernel-level file.
It controls system memory management, data input/output operations, and interrupt processing.
When Windows is started, kernel32.dll will reside in a specific write protection area in the memory, so that other programs cannot occupy this memory area.
Next, let's take a look at the prototype of the GetPrivateProfileString and WritePrivateProfileString functions.
DWORD GetPrivateProfileString (
Lptstr lpAppName, // section name
Lptstr lpKeyName, // key name
Lptstr lpDefault, // default string
LPTSTR lpReturnedString, // destination buffer
DWORD nSize, // size of destination buffer
LPCTSTR lpFileName // initialization file name
);
Parameter description:
LpAppName
[In] the pointer points to a null termination string, specifying the name containing the key name section. If this parameter is NULL, The GetPrivateProfileString function copies the name of all parts of the provided buffer.
LpKeyName
[In] indicates a null termination string. The name of the string key associated with it will be retrieved. If this parameter is NULL, all key names in the part specified by the AppName parameter are copied to the buffer zone specified by the returned string parameter.
LpDefault
[In] pointer to a null ending default string. If the lpKeyName key cannot be found in the initialization file, the default string of the copy of GetPrivateProfileString is the lpReturnedString buffer. This parameter cannot be NULL.
LpReturnedString
[Out] indicates the buffer that receives the retrieved string.
NSize
[In] specifies the size to return tchars. The buffer points to the lpReturnedString parameter.
LpFileName
[In] the pointer points to a null termination string, specifying the name of the initialization file. If this parameter does not contain the complete file path, the system searches for files in the Windows directory.
Return Value:
The return value is copied to the buffer, excluding the number of characters that terminate null characters.
**************************************** **************************************** **************************************** ***************
Several types of CSDN are described as follows:
DWORD: A 32-bit unsigned integer or the address of a segment and its associated offset.
LPCTSTR: A 32-bit pointer to a constant character string that is portable for Unicode and DBCS.
LPTSTR: A 32-bit pointer to a character string that is portable for Unicode and DBCS.
LPCSTR: A 32-bit pointer to a constant character string.
LPSTR: A 32-bit pointer to a character string.
Where:
L: long pointer.
P: indicates that this is a pointer.
T: represents _ T macro. This macro is used to indicate whether your character uses UNICODE. If your program defines UNICODE or other related macros, this character or string will be used as a UNICODE string, otherwise it will be a standard ANSI string.
STR: indicates that the variable is a string.
C: A constant, const.
**************************************** **************************************** **************************************** ***************
C # the corresponding relationship of the imported DLL type is as follows::
C ++: WORD ---- c #: ushort
C ++: DWORD ---- c #: uint
C ++: DWORD ---- c #: int
C ++: LPCTSTR ---- c #: StringBuilder
C ++: LPCTSTR ---- c #: string
C ++: LPCSTR (const char *) ---- c #: System. String
C ++: LPTSTR output variable name ---- c #: StringBuilder output variable name
Therefore, when C # imports the GetPrivateProfileString function, the int replaces the DWORD; the string replaces the LPCTSTR; and The StringBuilder output variable replaces the LPTSTR output variable.
It is concluded that:
DWORD GetPrivateProfileString (lpAppName, LPTSTR lpKeyName, LPTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPTSTR lpFileName );
Changed:
Public static extern int GetPrivateProfileString (string section, string key, string def, System. Text. StringBuilder retVal, int size, string filePath );
**************************************** **************************************** **************************************** *******************
Final use:
[System. Runtime. InteropServices. DllImport (kernel32)]
Public static extern int GetPrivateProfileString (string section, string key, string def, System. Text. StringBuilder retVal, int size, string filePath );
Public int ReadIni ()
{
String str = D: \ 123.ini;
StringBuilder temp = new StringBuilder ();
GetPrivateProfileString (ScreenOrder, LoginWindow, 1, temp, 255, str );
String str1 = temp. ToString ();
Int I = int. Parse (str1 );
Return I;
}
**************************************** **************************************** **************************************** *******************
The above describes how C # reads the. ini file. For C #, writing configuration files is very similar and will not be repeated.
Prototype:
BOOL WritePrivateProfileString (
Lptstr lpAppName, // section name
Lptstr lpKeyName, // key name
Lptstr lpString, // string to add
LPCTSTR lpFileName // initialization file
);
According to the type conversion of C # imported C ++ DLL described above, the functions in C # can be obtained as follows:
[System. Runtime. InteropServices. DllImport (kernel32)]
Public static extern int WritePrivateProfileString (string Section, string Key, string Value, string iniFilePath );