C # INI file operation class (simple configuration file) ini configuration file read class

Source: Internet
Author: User

You can use ini as the configuration file in a simple project. The operation class is as follows.

1 public class iniclass
2 {
3 Public String inipath;
4 [system. runtime. interopservices. dllimport ("Kernel32")]
5 Private Static extern long writeprivateprofilestring (string section, string key, string Val, string filepath );
6 [system. runtime. interopservices. dllimport ("Kernel32")]
7 Private Static extern int getprivateprofilestring (string section, string key, string def, stringbuilder retval, int size, string filepath );
8 /// <summary>
9 // Constructor
10 /// </Summary>
11 /// <Param name = "inipath"> file path </param>
12 public iniclass (string inipath)
13 {
14 inipath = inipath;
15}
16 /// <summary>
17 // write the INI File
18 /// </Summary>
19 /// <Param name = "section"> Project Name (for example, [typename]) </param>
20 /// <Param name = "key"> key </param>
21 /// <Param name = "value"> value </param>
22 public void iniwritevalue (string section, string key, string value)
23 {
24 writeprivateprofilestring (section, key, value, this. inipath );
25}
26 /// <summary>
27 // read the INI File
28 /// </Summary>
29 // <Param name = "section"> Project Name (for example, [typename]) </param>
30 /// <Param name = "key"> key </param>
31 Public String inireadvalue (string section, string key)
32 {
33 stringbuilder temp = new stringbuilder (500 );
34 int I = getprivateprofilestring (section, key, "", temp, 500, this. inipath );
35 return temp. tostring ();
36}
37 // <summary>
38 // verify whether the file exists
39 /// </Summary>
40 /// <returns> Boolean value </returns>
41 public bool existinifile ()
42 {
43 return file. exists (inipath );
44}
45}

 

Usage:

Iniclass ini = new iniclass (system. Windows. Forms. application. startuppath + "\ config. ini ");
M_parentdirpath = ini. inireadvalue ("road", "dir"). Trim (); // read
INI. iniwritevalue ("road", "dir", "d: \ AA"); // write

The content of the corresponding INI file is:

[road]
dir=D:\lzx\data

Refer:

INI File

Ini configuration file reading class

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.