How to read and write INI files in C #

Source: Internet
Author: User
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 the application by modifying the INI file. Program And many system configurations. 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 all the class libraries used in C # are hosted.Code(Managed code) files, while Win32 API function files 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:

C # declare the write operation function writeprivateprofilestring () of the INI file ():

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

C # declare the INI file's 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: Section: section name in the INI file; key: keyword in the INI file; Def: default value when the file cannot be read; retval: read value; Size: value size; filepath: the complete path and name of the INI file.

The following is a class of public class iniclass for reading and writing INI files.
{
Public String inipath;
[Dllimport ("Kernel32")]
Private Static extern long writeprivateprofilestring (string section, string key, string Val, string filepath );
[Dllimport ("Kernel32")]
Private Static extern int getprivateprofilestring (string section, string key, string def, stringbuilder retval, int size, string filepath );
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "inipath"> file path </param>
Public iniclass (string inipath)
{
Inipath = inipath;
}
/// <Summary>
/// Write the INI File
/// </Summary>
/// <Param name = "section"> Project Name (for example, [typename]) </param>
/// <Param name = "key"> key </param>
/// <Param name = "value"> value </param>
Public void iniwritevalue (string section, string key, string value)
{
Writeprivateprofilestring (section, key, value, this. inipath );
}
/// <Summary>
/// Read the INI File
/// </Summary>
/// <Param name = "section"> Project Name (for example, [typename]) </param>
/// <Param name = "key"> key </param>
Public String inireadvalue (string section, string key)
{
Stringbuilder temp = new stringbuilder (500 );
Int I = getprivateprofilestring (section, key, "", temp, 500, this. inipath );
Return temp. tostring ();
}
/// <Summary>
/// Verify whether the file exists
/// </Summary>
/// <Returns> Boolean value </returns>
Public bool existinifile ()
{
Return file. exists (inipath );
}
}

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.