C # _ read and write the ini configuration file

Source: Internet
Author: User

The. NET Framework class library does not provide the corresponding classes for reading and writing INI files. However, you can use WinAPI to read and write INI files. The code is very simple. As follows:

First, there are two API functions requiredPut it in your class and you can only do this,Put in method or (namespace outside the class), there will be a compilation Error:

1 using System.Runtime.InteropServices; 2 [DllImport( "kernel32" )] 3 private static extern long WritePrivateProfileString( string section, string key, string val, string filePath); 4 [DllImport( "kernel32" )] 5 private static extern long GetPrivateProfileString( string section, string key, string def,StringBuilder retVal, int size, string filePath);

 

WritePrivateProfileStringMethod description:
Function: write information to the INI file.

Return Value: long. If the value is 0, the write operation fails. Otherwise, the operation is successful.

Parameter 1 (section): name of a section written to the INI file (Case Insensitive ).

Parameter 2 (key): The key name of an item in the preceding section (Case Insensitive ).

Parameter 3 (val): value corresponding to the above key

Parameter 4 (filePath): ini file name, including its path (example: "c: config. ini "). If no path is specified and only the file name is specified, the system will automatically find whether there is a corresponding INI file in the windows directory. If not, the system will automatically create an INI file in the root directory where the current application is running.

 

INI file structure Example:

[JXCDB] -- section)

Server = 192.168.1.1 -- server is a key under JXCDB, and 192.168.1.1 is the server key value (the same below)

Name = sa

Pwd = 1, 198910

DbName = JXC

 

Use the GetPrivateProfileString method:
Function: read information from the INI file.
Return Value: returns the length of the byte string of the obtained information.

  • Parameter 1 (section): name of a section (case-insensitive). If it is null, a list of all sections of the INI file will be loaded in retVal.
  • Parameter 2 (key): key name (case-insensitive) of the information to be obtained. If it is null, a list of all keys in the specified section will be loaded in retVal.
  • Parameter 3 (def): If the specified information is not found, def is returned, which can be null.
  • Parameter 4 (retVal): A string buffer. The obtained string is saved in the buffer. The buffer size must be at least size.
  • Parameter 5 (size): the buffer size of retVal (maximum number of characters ).
  • Parameter 6 (filePath): Specifies the INI file path. If no path exists, search in the windows directory. If not, search in the application directory, only def is returned.

Use Example in detail:

First, create an INI file and save the information.:

1 WritePrivateProfileString( "JXCDB" , "server" , "." , Application.StartupPath + "\JXC_Server.ini" ); 2 WritePrivateProfileString( "JXCDB" , "name" , txtName.Text.Trim(), Application.StartupPath + "\JXC_Server.ini" ); 3 WritePrivateProfileString( "JXCDB" , "pwd" ,txtPwd.Text.Trim(), Application.StartupPath + "\JXC_Server.ini" ); 4 WritePrivateProfileString( "

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.