C # uses WINAPI for read write configuration files

Source: Internet
Author: User
Tags config ini read ini file

Our C # Read and write configuration files are usually read and written to files with IO. I think using WINAPI's Kernel32.dll to read INI file is better and more convenient

Read and write to INI file we can use the WritePrivateProfileString (write) in Kernel32.dll, GetPrivateProfileString (read in string), Getprivateprofileint (Read integer value) three methods. I'm here to introduce three methods.

I. Write information to the. ini file.

1. The WINAPI function used is: writeprivateprofilestring

[DllImport ("Kernel32.dll")]

public static extern bool WritePrivateProfileString (string strappname,

String Strkeyname,

String strstring,

String strFileName);

The meaning of each of these parameters:

String Strappname, is a field name in the INI file.

String Strkeyname, is a strappname under the name of a key, popularly speaking is the variable name.

String strstring, is a key value, which is the value of the variable (string type)

String strFileName is the full INI file path name.

2. The specific use method: I have a Web page refresh device. I want to save the Web page (URL) that I set up, and the interval (time)

WritePrivateProfileString ("Settings", "Url", "http://wenti.cnblogs.com", "C:\\config.ini");

WritePrivateProfileString ("Settings", "Time", "a", "C:\\config.ini");

The contents of the saved INI file are as follows:

[Settings]

Url=http://wenti.cnblogs.com

Time=20

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.