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