-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigOperation.cs
68 lines (63 loc) · 1.46 KB
/
ConfigOperation.cs
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
using System;
using System.Configuration;
using System.Xml;
internal class ConfigOperation
{
public static string GetConnectionString(string ConnName)
{
return ConfigurationManager.ConnectionStrings[ConnName].ConnectionString;
}
public static string GetAppSettings(string SettingName)
{
try
{
return ConfigurationManager.AppSettings[SettingName].ToString();
}
catch (Exception ex)
{
return ex.Message;
}
}
public static string GetAppSettings(string configFile, string SettingName)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(configFile);
XmlNode xmlNode = xmlDocument.DocumentElement.SelectSingleNode("//add[@key='" + SettingName + "']");
if (xmlNode == null)
{
return null;
}
return xmlNode.Attributes["value"].Value;
}
catch
{
return null;
}
}
public static bool SetAppSettings(string AppKey, string AppValue)
{
return SetAppSettings(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, AppKey, AppValue);
}
public static bool SetAppSettings(string configFile, string AppKey, string AppValue)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(configFile);
XmlNode xmlNode = xmlDocument.DocumentElement.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xmlNode == null)
{
return false;
}
xmlNode.Attributes["value"].Value = AppValue;
xmlDocument.Save(configFile);
return true;
}
catch
{
return false;
}
}
}