Use C # To Read and Write ini configuration files

Source: Internet
Author: User

INI is a file with the extension "INI". In fact, INI is a text file. It can work in notepad and mainly stores user choices or various system parameters.
The INI file is not a common text file. it has its own structure. it consists of several sections. Under each title with parentheses, It is a number of keywords that begin with a single word (KEYWORD) and an equal sign, the right side of the equal sign is the VALUE of the keyword ). for example:
[Section1]
KeyWord1 = Value1
KeyWord2 = Value2
...
[Section2]
KeyWord3 = Value3
KeyWord4 = Value4

C # The namespace does not directly read and write INI classes. Of course, if you use INT as a text file and use the System. IO class to read and write data, I didn't say it.
I am introducing the system's INI processing method.
Although C # does not exist, there are Win32 API functions in the "kernel32.dll" file -- WritePrivateProfileString () and GetPrivateProfileString ()
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 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 for reading and writing INI files:
 

Public class INIClass
{
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;
}
 
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.