[C #]: How to read the INI file in C # (1)

zhaozj2021-02-16  59

[C #]: how to read and write INI file in C # Author: Ma Jinhu article taken from: SEOUL

2002

Year 09

Month 24

day

The INI file is an extension of the file named "ini". In the Windows system, INI files are many, the most important thing is "System.ini", "System32.ini" and "Win.ini". This file mainly stores the selection of users and various parameters of the system. Users can change many configurations of the application and system by modifying the INI file. However, since the exit of Windows 95, the concept of the registry is introduced in the Windows system. The status of the Ini file will be continuously declining in the status of the Windows system, which is because the unique advantages of the registry makes the applications and systems to initialize many parameters and initialization. The information is placed in the registry. But in some occasions, the INI file also has its irreplaceable position. This article discusses how C # is how to read and write in INI.

INI file structure

The INI file is a text file arranged in a feature manner. Each INI file is very similar, consisting of several paragraphs, under each heading header, is a keyword that starts with a single word (keyword) and a equal sign, the right number is key. Value. The general form is as follows:

[Section1]

Keyword1 = Valuel

Keyword2 = Value2

......

[Section2]

Keyword3 = Value3

Keyword4 = Value4

Program design and operation environment described in this article: ● Microsoft Window 2000 Advanced Server Edition ● .NET Framework SDK official version

C # and Win32 API functions

C # is not like C , has its own class library. C # Used by the class library is a copk, which is available for all .NET programs --.NET Framework SDK. Although the contents of the .NET Framework SDK is very large, the function is also very powerful, but it is not enough, at least it does not provide the related classes needed directly to operate the INI file. In this article, the C # Operation INI file is used by the Windows system comes with Win32 API functions --WritePrivateProfileString () and getPrivateProfileString () functions. These two functions are located in the "kernel32.dll" file. We know that the class libraries used in C # are managed code files, while the files in the Win32 API function are unmanaged code files. This results in a function in which these non-hosting code files cannot be used directly in C #. It is preferred to maintain the next compatibility in order to maintain the next compatibility, and the interoperability is proposed, and the call to the WIN32's API function can be implemented by interoperability. Interoperability not only applies to Win32 API functions, but also use to access managed COM objects. Interoperability of the API function for WIN32 in C # is implemented by the "DLLIMPORT" feature class in the namespace "System.Runtime.InterOpServices". Its main role is to indicate that this property is implemented as an output of the non-host DLL. The following code is the "Dllimport" feature class in C # using the namespace "DLLIMPORT" feature classes in the name of the two WIN32 API functions: C # declares the write operation function of the INI file WritePrivateProfileString (): [DLLIMPORT ("kernel32 ")]

Private static extern long writPrivateProfileString (String

Section,

String Key, String Val, String FilePath;

Parameter Description: Section: Ini files in the paragraph; key: INI file; values ​​in the keyword in the INI file; FilePath: INI file full path and name. C # declares the read operation function of the INI file GetPrivateProfileString ():

[DLLIMPORT ("kernel32")]]

Private static extern int GETPRIVATEPROFILESTRING (String Section,

String Key, String Def, StringBuilder RetVal,

INT size, String filepath;

Parameter Description: Section: The paragraph name in the INI file; key: INI file; DEF: The default value when it is not read; RetVal: Read the value; size: Numerical size; filepath: ini file The full path and name.

转载请注明原文地址:https://www.9cbs.com/read-22181.html

New Post(0)