[C #]: How to read and write the INI file in C # (i)

Source: Internet
Author: User
Tags ini int size win32
INI file is a file with the extension "INI". In the Windows system, the INI file is a lot, the most important thing is "System.ini", "System32.ini" and "Win.ini". This file mainly holds the selections made by the user and the various parameters of the system. Users can modify the INI file to change many of the application and system configuration. But since Windows 95 's exit, the concept of the Registry has been introduced into Windows, and the INI file has been declining in the Windows system because of the unique advantages of the registry, which allows applications and systems to put many parameters and initialization information into the registry. But in some cases, the INI file also has its irreplaceable status. This article will discuss how C # is read and write to the INI.

The structure of the INI file



An INI file is a text file that is arranged in a way that is characteristic. Each INI file composition is very similar, consists of several paragraphs (section), under each parenthesized heading, is a number of words beginning with a single word (keyword) and an equal sign, the equal sign to the right is the value of the keyword (value). The general form is as follows:




[Section1]

KeyWord1 = Valuel

KeyWord2 = Value2

......

[Section2]

KeyWord3 = Value3

KeyWord4 = Value4




The program design and operating environment described in this paper:

Microsoft Windows 2000 Advanced Server Edition

.net Framework SDK Official Edition

C # and Win32 API functions



C # does not have a class library of its own, like C + +. The class library used by C # is a common class library ——. NET Framework SDK that is provided by the. NET Framework for all. NET program development. Although the. Net FrameWork SDK is very large and powerful, it is not exhaustive, at least it does not provide the related classes needed to directly manipulate the INI file. In this article, the C # operations INI file uses the API functions--writeprivateprofilestring () and getprivateprofilestring () functions that are Win32 by the Windows system. These two functions are located in the "Kernel32.dll" file.

We know that the class libraries used in C # are managed code (Managed code) files, and Win32 API functions are files that are unmanaged (unmanaged code) files. This leads to the impossibility of directly using functions in C # in these unmanaged code files. Fortunately. NET Framework in order to maintain the compatibility of the next, also in order to make full use of the previous resources, the interoperability, through interoperability can be implemented on the WIN32 API function calls. interoperation applies not only to Win32 API functions, but also to managed COM objects. The interoperability of WIN32 API functions in C # is achieved through the "DllImport" feature class in the namespace "System.Runtime.InteropServices". Its primary role is to indicate that this attributed method is implemented as an output of an unmanaged DLL. The following code uses the "DllImport" feature class in the namespace "System.Runtime.InteropServices" in C # to declare the above two WIN32 API functions:

C # declaration INI file write operation function writeprivateprofilestring ():




[DllImport ("kernel32")]

private static extern long WritePrivateProfileString (string

Section,

String key, String val, string filePath);




Parameter description: The paragraph in the Section:ini file, the keywords in the Key:ini file, the number of keywords in the Val:ini file, and the full path and name of the Filepath:ini file.

C # declaration INI file 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: The paragraph name in the Section:ini file; key:ini the keyword in the file; def: the default value when it cannot be read; RetVal: reads the value, size: The value, the full path and name of the Filepath:ini file.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.