Ways to read and write INI files in C #

Source: Internet
Author: User
Tags ini

C # usually uses an xml-based configuration file, but if necessary, such as taking care of older systems, you might want to use the INI file.

But C # itself does not have the API to read and write the INI file, only by invoking unmanaged code, the system's own APIs to achieve the desired purpose.

The corresponding methods for reading and writing are getprivateprofilestring and writeprivateprofilestring respectively.

The parameters in the GetPrivateProfileString:

Name of Lpappname--section

Name of Lpkeyname--key

lpdefault--if Lpkeyname is not found, copy this value into lpreturnedstring

lpreturnedstring--value used to return the result

Character length of nsize--lpreturnedstring

Lpfilename--ini file name

The parameters in the WritePrivateProfileString:

Name of Lpappname--section

Name of Lpkeyname--key

lpstring--values corresponding to Lpkeyname

Lpfilename--ini file name

The actual code looks like this:

Using System;  
Using System.Runtime.InteropServices;  
      
Using System.Text; Namespace Inidemo {class Program {static void Main (string[] args) {writ  
         Eprivateprofilestring ("Demo", "abc", "123", "C:\\demo.ini");  
            See more highlights of this column: Http://www.bianceng.cn/Programming/csharp/StringBuilder temp = new StringBuilder ();  
            GetPrivateProfileString ("Demo", "abc", "" ", temp, 255," C:\\demo.ini ");  
      
            Console.WriteLine (temp);  
        Console.ReadLine ();  } [DllImport ("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern  
      
        BOOL WritePrivateProfileString (String lpappname, String lpkeyname, String lpstring, string lpfilename); [DllImport ("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern in T GetPrivateProfileString (String lpappname, String LpkeYname, String lpdefault, StringBuilder lpreturnedstring, int nsize, string lpfilename); }  
}

The content in the INI file after the program runs is:

[Demo]

Abc=123

This is a relatively simple approach, and if you don't want to use an unmanaged method, you can solve the problem in a different, more troublesome way.

Because the format of the INI file is fixed, so long as the corresponding parsing program can complete the same read and write functions, is the usual string processing.

If you don't want to do it yourself, it doesn't matter, there is already a ready-made program--cinchoo framework that can help you achieve what you want to do.

And then it all gets easier.

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.