An ini file is a file with the extension "ini. In Windows, there are many ini files. The most important ones are System. ini, System32.ini, and Win. ini ". This file mainly stores user selection and various system parameters. You can change many configurations of applications and systems by modifying the INI file. However, since Windows 95 exit, the registry concept has been introduced in Windows, and the position of INI files in Windows has been declining. This is due to the unique advantages of the Registry, the application and system put many parameters and initialization information into the registry. However, in some cases, INI files are irreplaceable. This article will discuss how C # reads and writes INI.
INI file structure
INI files are text files arranged according to their characteristics. Each INI file is very similar and consists of several sections. Under each title with parentheses, there are several keywords starting with a single word (keyword) and an equal sign, the right side of the equal sign is the value corresponding to the keyword ). The general form is as follows:
[Section1]
KeyWord1 = Valuel
KeyWord2 = Value2
......
[Section2]
KeyWord3 = Value3
KeyWord4 = Value4
C # And Win32 API functions
C # has its own class library, not like C ++. C # The class library used is a common class library provided by the. Net FrameWork for all. Net Program Development-. Net FrameWork SDK. Although the. Net FrameWork SDK has a huge amount of content and powerful functions, it cannot cover all aspects. At least it does not provide the relevant classes required to directly operate the INI file. In this article, C # uses the Win32 API function -- WritePrivateProfileString () and GetPrivateProfileString () in Windows to operate the INI file. Both functions are located in the "kernel32.dll" file.
We know that the class libraries used in C # are Managed Code files, while the files of Win32 API functions are all Unmanaged Code files. This makes it impossible to directly use the functions in these unmanaged code files in C. Fortunately, in order to maintain compatibility with the. Net Framework, and to make full use of previous resources, the interoperability is proposed. Through interoperability, Win32 API functions can be called. Interoperability not only applies to Win32 API functions, but also can be used to access hosted COM objects. In C #, the interoperability of Win32 API functions is achieved through the "DllImport" feature class in the namespace "System. Runtime. InteropServices. Its main function is to indicate that this attribute method is implemented as the output of the unmanaged DLL. The following Code declares the preceding two Win32 API functions in C # using the "DllImport" feature class in the namespace "System. Runtime. InteropServices: