-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConfigClass.cs
More file actions
135 lines (114 loc) · 3.51 KB
/
ConfigClass.cs
File metadata and controls
135 lines (114 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System.Runtime.InteropServices;
using System.Text;
public class IniConfig
{
/*
* 声明API函数
*/
public string iniPath;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal,
int size, string filePath);
// [DllImport("kernel32")]
// private static extern int GetPrivateProfileInt(string section, string key, int def, string filePath);
#region Ini文件管理
/// <summary>
/// 构造函数
/// </summary>
/// <param name="iniPath">ini文件路径,默认为当前路径下config.ini</param>
// public IniConfig(string path = "./config.ini")
public IniConfig(string path)
{
// AppDomain.CurrentDomain.SetupInformation.ApplicationBase
iniPath = path;
}
/// <summary>
/// 查询ini文件是否存在
/// </summary>
/// <returns>是否存在</returns>
public bool FileExist()
{
return File.Exists(iniPath);
}
#endregion
#region 默认default节内的键值对操作
/// <summary>
/// 读取section,不管section,默认从default里读取
/// </summary>
/// <param name="Key">键</param>
/// <returns>返回值</returns>
public string ReadKey(string Key)
{
return ReadKey("default", Key);
}
/// <summary>
/// 写入ini文件,不管section,默认放在default里
/// </summary>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public void WriteKey(string Key, string Value)
{
WritePrivateProfileString("default", Key, Value, iniPath);
}
/// <summary>
/// 删除默认default节的key
/// </summary>
/// <param name="Key"></param>
public void DeleteKey(string Key)
{
WritePrivateProfileString("default", Key, null, iniPath);
}
#endregion
#region 指定名称的节内的键值对操作
/// <summary>
/// 读取ini文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">键</param>
/// <returns>返回的值</returns>
public string ReadKey(string Section, string Key)
{
StringBuilder temp = new StringBuilder(256);
GetPrivateProfileString(Section, Key, "", temp, 256, iniPath);
return temp.ToString();
}
/// <summary>
/// 写入ini文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public void WriteKey(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, iniPath);
}
/// <summary>
/// 删除指定的key
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
public void DeleteKey(string Section, string Key)
{
WritePrivateProfileString(Section, Key, null, iniPath);
}
#endregion
#region 删除节操作
/// <summary>
/// 删除ini文件下personal段落下的所有键
/// </summary>
/// <param name="Section"></param>
public void DeleteSection(string Section)
{
WriteKey(Section, null, null);
}
/// <summary>
/// 删除ini文件下所有段落
/// </summary>
public void DeleteAllSection()
{
WriteKey(null, null, null);
}
#endregion
}