C # INI file operation [Source Code download ],

Source: Internet
Author: User

C # INI file operation [Source Code download ],

This section describes how to read and write INI files. C # You can call the WritePrivateProfileString () and GetPrivateProfileString () functions in the [kernel32.dll] file to read and write INI files respectively. Including reading key values, saving key values, reading all sections, reading all keys, removing sections, and removing keys.

Directory

1. Introduction to the INI File

2. Read operations: including reading key values, reading all sections, and reading all keys.

3. Write operations: Including saving key values, removing sections, and removing keys.

4. Download source code: display the runtime diagram and download the source code

 

1. Introduction to the INI File

INI files are often used to store configuration information of various applications. The internal file structure mainly includes three concepts:Section,KeyAndValue.

WhereSectionIt is an independent area block and can be named in English or Chinese.

 

2. GetPrivateProfileString () function: read operation

C # You can callGetPrivateProfileString() Function to read INI files.

Official API: Https://msdn.microsoft.com/zh-cn/library/ms724353.aspx

Function Signature:

[DllImport("kernel32")]private static extern int GetPrivateProfileString(string sectionName, string key, string defaultValue, byte[] returnBuffer, int size, string filePath); 

Member:

SectionName {string | null}: name of the region to be read. If a null value is input, all section names are obtained for the 4th returnBuffer parameter.

Key {string | null}: name of the key. If a null value is input, the 4th parameter returnBuffer will obtain all key names under all specified sectionnames.

DefaultValue {string}: return value when the key is not found.

ReturnBuffer {byte []}: value corresponding to the key.

FilePath {string}: INI file path.

Supported operations:

1) obtain the value of the specified key.

2) obtain the names of all sections in the INI file.

3) obtain the names of all keys in a specified section.

 

2.1 obtain the value of the specified key
/// <Summary> /// read Value based on Key /// </summary> /// <param name = "sectionName"> section name </param> /// <param name = "key"> key name </param> // <param name = "filePath"> file path </param> public static string GetValue (string sectionName, string key, string filePath) {byte [] buffer = new byte [2048]; int length = GetPrivateProfileString (sectionName, key, "error", buffer, 999, filePath ); string rs = System. text. UTF8Encoding. default. getString (buffer, 0, length); return rs ;}

 

2.2 obtain the names of all sections in the INI File

Note:: The section with the Chinese name needs to be transcoded.

/// <Summary> /// obtain the names of all sections in the INI file /// </summary> /// <param name = "filePath"> file path </param> /// <returns> returns a set of section names </returns> public static List <string> GetSectionNames (string filePath) {byte [] buffer = new byte [2048]; int length = GetPrivateProfileString (null, "", "", buffer, 999, filePath); String [] rs = System. text. UTF8Encoding. default. getString (buffer, 0, length ). split (new string [] {"\ 0"}, StringSplitOptions. removeEmptyEntries); return rs. toList ();}

  

2.3 obtain the names of all keys in a specified section

Transcode the key of the name in the question.

/// <Summary> /// obtain all the keys in the specified section /// </summary> /// <param name = "sectionName"> section name </param> /// <param name = "filePath"> file path </param> /// <returns> returns a set of key names </returns> public static List <string> getKeys (string sectionName, string filePath) {byte [] buffer = new byte [2048]; int length = GetPrivateProfileString (sectionName, null, "", buffer, 999, filePath ); string [] rs = System. text. UTF8Encoding. default. getString (buffer, 0, length ). split (new string [] {"\ 0"}, StringSplitOptions. removeEmptyEntries); return rs. toList ();}

 

3. WritePrivateProfileString () function: write operation

C # You can callWritePrivateProfileString() The function writes the INI file.

Official API: Https://msdn.microsoft.com/zh-cn/library/ms725501.aspx

Function Signature:

[DllImport("kernel32")]private static extern long WritePrivateProfileString(string sectionName, string key, string value, string filePath);

Member:

SectionName {string}: name of the region to be written.

Key {string | null}: name of the key. If a null value is input, the specified section is removed.

Value {string | null}: set the value corresponding to the key. If a null value is input, the specified key is removed.

FilePath {string}: INI file path.

Supported operations:

1) Create/set the key value.

2) Remove the specified section.

3) Remove the specified key.

 

3.1 create/set the key value

Note:: If this key does not exist, it will be created; otherwise, it will be changed to the value of this key.

/// <Summary> /// Save the content to the INI file /// <para> If the same key exists, overwrite it, otherwise, add </para> /// </summary> /// <param name = "sectionName"> section name </param> /// <param name = "key "> key name </param> /// <param name =" value "> stored value </param> /// <param name =" filePath "> file path </param> public static bool SetValue (string sectionName, string key, string value, string filePath) {int rs = (int) WritePrivateProfileString (sectionName, key, value, filePath); return rs> 0 ;}

 

3.2 remove the specified section

Description: If the key parameter is null, the specified section is removed.

/// <Summary> /// remove the specified section // </summary> /// <param name = "sectionName"> section name </param> /// <param name = "filePath"> file path </param> // <returns> </returns> public static bool RemoveSection (string sectionName, string filePath) {int rs = (int) WritePrivateProfileString (sectionName, null, "", filePath); return rs> 0 ;}

  

3.3 remove the specified key

Description: If the value parameter is null, the specified key is removed.

/// <Summary> /// remove the specified key /// </summary> /// <param name = "sectionName"> section name </param> /// <param name = "filePath"> file path </param> // <returns> </returns> public static bool Removekey (string sectionName, string key, string filePath) {int rs = (int) WritePrivateProfileString (sectionName, key, null, filePath); return rs> 0 ;}

 

4. Download the source code 4.1.

 

4.2

Baidu online storage: Http://pan.baidu.com/s/1dEQ3QuP

CSDN: Http://download.csdn.net/detail/polk6/9684148

 

====================================== Series of articles ==============================================

This article: 3.4 C # INI File Operations [Source Code download]

C # Article navigation

 

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.