之前文章:《两种读写配置文件的方案(app.config与web.config通用)》,现在重新整理一个更完善的版本,增加批量读写以及指定配置文件路径,代码如下:
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Configuration;
using
System.IO;
namespace
Zuowj.Utils
{
/// <summary>
/// 配置工具类
/// Author:左文俊
/// Date:2018/1/27
public
static
class
ConfigUtil
{
/// <summary>
/// 获取管理配置文件对象Configuration
/// </summary>
/// <param name="configPath">指定要管理的配置文件路径,如果为空或不存在,则管理程序集默认的配置文件路径</param>
/// <returns></returns>
private
static
Configuration GetConfiguration(
string
configPath =
null
)
{
if
(!
string
.IsNullOrEmpty(configPath) && File.Exists(configPath))
{
ExeConfigurationFileMap map =
new
ExeConfigurationFileMap();
map.ExeConfigFilename = configPath;
return
ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}
else
{
return
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
}
/// <summary>
/// 获取指定配置文件+配置名称的配置项的值
/// </summary>
public
static
string
GetAppSettingValue(
string
key,
string
defaultValue =
null
,
string
configPath =
null
)
{
var
config = GetConfiguration(configPath);
var
appSetting = config.AppSettings.Settings[key];
return
appSetting.Value;
}
/// <summary>
/// 设置配置值(存在则更新,不存在则新增)
/// </summary>
public
static
void
SetAppSettingValue(
string
key,
string
value,
string
configPath =
null
)
{
var
config = GetConfiguration(configPath);
var
setting = config.AppSettings.Settings[key];
if
(setting ==
null
)
{
config.AppSettings.Settings.Add(key, value);
}
else
{
setting.Value = value;
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(
"appSettings"
);
}
/// <summary>
/// 删除配置值
/// </summary>
public
static
void
RemoveAppSetting(
string
key,
string
configPath =
null
)
{
var
config = GetConfiguration(configPath);
config.AppSettings.Settings.Remove(key);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(
"appSettings"
);
}
/// <summary>
/// 设置多个配置值(存在则更新,不存在则新增)
/// </summary>
public
static
void
SetAppSettingValues(IEnumerable<KeyValuePair<
string
,
string
>> settingValues,
string
configPath =
null
)
{
var
config = GetConfiguration(configPath);
foreach
(
var
item
in
settingValues)
{
var
setting = config.AppSettings.Settings[item.Key];
if
(setting ==
null
)
{
config.AppSettings.Settings.Add(item.Key, item.Value);
}
else
{
setting.Value = item.Value;
}
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(
"appSettings"
);
}
/// <summary>
/// 获取所有配置值
/// </summary>
public
static
Dictionary<
string
,
string
> GetAppSettingValues(
string
configPath =
null
)
{
Dictionary<
string
,
string
> settingDic =
new
Dictionary<
string
,
string
>();
var
config = GetConfiguration(configPath);
var
settings = config.AppSettings.Settings;
foreach
(
string
key
in
settings.AllKeys)
{
settingDic[key] = settings[key].ToString();
}
return
settingDic;
}
/// <summary>
/// 删除多个配置值
/// </summary>
public
static
void
RemoveAppSettings(
string
configPath =
null
,
params
string
[] keys)
{
var
config = GetConfiguration(configPath);
if
(keys !=
null
)
{
foreach
(
string
key
in
keys)
{
config.AppSettings.Settings.Remove(key);
}
}
else
{
config.AppSettings.Settings.Clear();
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(
"appSettings"
);
}
/// <summary>
/// 获取连接字符串
/// </summary>
public
static
string
GetConnectionString(
string
name,
string
defaultconnStr =
null
,
string
configPath =
null
)
{
var
config = GetConfiguration(configPath);
var
connStrSettings = config.ConnectionStrings.ConnectionStrings[name];
if
(connStrSettings ==
null
)
{
return
defaultconnStr;
}
return
connStrSettings.ConnectionString;
}
/// <summary>
/// 获取指定配置文件+连接名称的连接字符串配置项
/// </summary>
public
static
ConnectionStringSettings GetConnectionStringSetting(
string
name,
string
configPath =
null
)
{
var
config = GetConfiguration(configPath);
var
connStrSettings = config.ConnectionStrings.ConnectionStrings[name];
return
connStrSettings;
}
/// <summary>
/// 设置连接字符串的值(存在则更新,不存在则新增)
/// </summary>
public
static
void
SetConnectionString(
string
name,
string
connstr,
string
provider,
string
configPath =
null
)
{
var
config = GetConfiguration(configPath);
ConnectionStringSettings connStrSettings = config.ConnectionStrings.ConnectionStrings[name];
if
(connStrSettings !=
null
)
{
connStrSettings.ConnectionString = connstr;
connStrSettings.ProviderName = provider;
}
else
{
connStrSettings =
new
ConnectionStringSettings(name, connstr, provider);
config.ConnectionStrings.ConnectionStrings.Add(connStrSettings);
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(
"connectionStrings"
);
}
/// <summary>
/// 删除连接字符串配置项
/// </summary>
public
static
void
RemoveConnectionString(
string
name,
string
configPath =
null
)
{
var
config = GetConfiguration(configPath);
config.ConnectionStrings.ConnectionStrings.Remove(name);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(
"connectionStrings"
);
}
/// <summary>
/// 获取所有的连接字符串配置项
/// </summary>
public
static
Dictionary<
string
, ConnectionStringSettings> GetConnectionStringSettings(
string
configPath =
null
)
{
var
config = GetConfiguration(configPath);
var
connStrSettingDic =
new
Dictionary<
string
, ConnectionStringSettings>();
var
connStrSettings = ConfigurationManager.ConnectionStrings;
foreach
(ConnectionStringSettings item
in
connStrSettings)
{
connStrSettingDic[item.Name] = item;
}
return
connStrSettingDic;
}
/// <summary>
/// 设置多个连接字符串的值(存在则更新,不存在则新增)
/// </summary>
public
static
void
SetConnectionStrings(IEnumerable<ConnectionStringSettings> connStrSettings,
string
configPath =
null
)
{
var
config = GetConfiguration(configPath);
foreach
(
var
item
in
connStrSettings)
{
ConnectionStringSettings connStrSetting = config.ConnectionStrings.ConnectionStrings[item.Name];
if
(connStrSetting !=
null
)
{
connStrSetting.ConnectionString = item.ConnectionString;
connStrSetting.ProviderName = item.ProviderName;
}
else
{
config.ConnectionStrings.ConnectionStrings.Add(item);
}
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(
"connectionStrings"
);
}
/// <summary>
/// 删除多个连接字符串配置项
/// </summary>
public
static
void
RemoveConnectionStrings(
string
configPath =
null
,
params
string
[] names)
{
var
config = GetConfiguration(configPath);
if
(names !=
null
)
{
foreach
(
string
name
in
names)
{
config.ConnectionStrings.ConnectionStrings.Remove(name);
}
}
else
{
config.ConnectionStrings.ConnectionStrings.Clear();
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(
"connectionStrings"
);
}
}
}
|
本文转自 梦在旅途 博客园博客,原文链接:http://www.cnblogs.com/zuowj/p/8366735.html ,如需转载请自行联系原作者