C # Read and Write ini files, c # Read and Write ini

Source: Internet
Author: User

C # Read and Write ini files, c # Read and Write ini

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 # The INI file is operated using the Windows built-in Win32 API functions WritePrivateProfileString () and GetPrivateProfileString. Both functions are located in the "kernel32.dll" file.

C # use the "DllImport" feature class in the namespace "System. Runtime. InteropServices" to declare the preceding two Win32 API functions:

[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: the value of the keyword in the INI file; filePath: the complete path and name of the INI file.

[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: the keyword in the INI file;

Def: the default value when it cannot be read; retVal: read value; size: value size;

FilePath: the complete path and name of the INI file.

Write the INI file. If the INI file contains the same paragraph name and keyword as the information to be written, the INI information will be overwritten.

Correct INI reading must meet three prerequisites: full path of the INI file, paragraph name, and keyword name.

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.