-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAppSettings.cs
156 lines (132 loc) · 3.31 KB
/
AppSettings.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
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
using System;
using System.Xml;
using System.Configuration;
using System.Reflection;
using System.IO;
namespace Subindex
{
/// <summary>
///
/// </summary>
public class AppSettings
{
private static readonly string configFile;
private AppSettings()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
static AppSettings()
{
configFile=getConfigFilePath();
}
// public static string ReadSetting(string key)
// {
// string s=null;
// try
// {
//
// s= ConfigurationSettings.AppSettings[key];
// }
// catch (Exception e)
// {
// System.Windows.Forms.MessageBox.Show(e.Message);
// }
// return s;
// }
public static string ReadSetting(string Key)
{
string Path="/configuration/appSettings/add";
XmlDocument doc = loadConfigDocument();
//if (doc.SelectSingleNode(Path) == null) return null;
XmlElement elem = (XmlElement)doc.SelectSingleNode(string.Format(Path+"[@key='{0}']", Key));
return elem==null?null:elem.GetAttribute("value");;
}
public static void WriteSetting(string Key, string Value)
{
XmlDocument doc = loadConfigDocument();
if (doc.SelectSingleNode("//configuration") == null)
doc.AppendChild(doc.CreateNode(XmlNodeType.Element,"configuration",""));
XmlNode node = doc.SelectSingleNode("//appSettings");
if (node == null)
{
node=doc.CreateNode(XmlNodeType.Element,"appSettings","");
doc.SelectSingleNode("//configuration").AppendChild(node);
}
try
{
XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", Key));
if (elem != null)
{
elem.SetAttribute("value", Value);
}
else
{
elem = doc.CreateElement("add");
elem.SetAttribute("key", Key);
elem.SetAttribute("value", Value);
node.AppendChild(elem);
}
doc.Save(configFile);
}
catch
{
throw;
}
}
public static void RemoveSetting(string key)
{
XmlDocument doc = loadConfigDocument();
XmlNode node = doc.SelectSingleNode("//appSettings");
try
{
if (node == null)
throw new InvalidOperationException("appSettings section not found in config file.");
else
{
node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']", key)));
doc.Save(configFile);
}
}
catch (NullReferenceException e)
{
throw new Exception(string.Format("The key {0} does not exist.", key), e);
}
}
private static XmlDocument loadConfigDocument()
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(configFile);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
return doc;
}
public static string getConfigFilePath()
{
string configFile=Assembly.GetExecutingAssembly().Location + ".config";
if (!File.Exists(configFile)) CreateFile(configFile);
return configFile;
}
public static bool ConfigFileExist()
{
string configFile=Assembly.GetExecutingAssembly().Location + ".config";
return File.Exists(configFile);
}
private static void CreateFile(string FileName)
{
XmlTextWriter writer = new XmlTextWriter(FileName,System.Text.Encoding.Default);
writer.WriteStartElement("configuration");
writer.WriteStartElement("appSettings");
writer.WriteEndElement() ;
writer.Flush();
writer.Close();
}
}
}