1
using System;
2
using System.IO;
3
using System.Xml;
4
using System.Collections;
5
using System.Windows.Forms;
6
namespace SSSSystem.Class
7

{
8
/**//// <summary>
9
/// ConfigSettings 的摘要说明。
10
/// </summary>
11
public class ConfigSettings
12
{
13
public ConfigSettings()
14
{
15
//
16
// TODO: 在此处添加构造函数逻辑
17
//
18
InitFilePath();
19
Read();
20
}
21
22
私有成员#region 私有成员
23
private Hashtable list = new Hashtable();
24
private string filePath="";
25
private bool autoWrite=true;
26
private string[,] _defaultValues;
27
#endregion
28
29
属性#region 属性
30
public bool AutoWrite
31
{
32
get
{return autoWrite;}
33
set
{autoWrite=value;}
34
}
35
36
public string FilePath
37
{
38
get
39
{
40
string folderPath=Path.GetDirectoryName(filePath);
41
if(Directory.Exists(folderPath)==false)
42
{
43
Directory.CreateDirectory(folderPath);
44
}
45
return filePath;
46
}
47
set
{filePath=value;}
48
}
49
#endregion
50
51
public ConfigSettings(string[,] defaultValues)
52
{
53
_defaultValues=defaultValues;
54
InitFilePath();
55
Read();
56
}
57
public void SetValue(object key,Object _value)
58
{
59
if(! list.Contains(key.ToString()))
60
{
61
throw new Exception("配置文件中没有指定项");
62
}
63
list[key.ToString()] = _value;
64
65
if(autoWrite==true)
66
{
67
Write();
68
}
69
}
70
71
public void AddValue(object key,Object _value)
72
{
73
list.Add(key.ToString(),_value);
74
75
if(autoWrite==true)
76
{
77
Write();
78
}
79
}
80
81
public void RemoveValue(object key)
82
{
83
list.Remove(key.ToString());
84
85
if(autoWrite==true)
86
{
87
Write();
88
}
89
}
90
91
92
public string GetString(object key)
93
{
94
string keyStr = key.ToString();
95
Object result=list[keyStr];
96
if(result==null)
97
{
98
return "";
99
}
100
else
101
{
102
return result.ToString();
103
}
104
}
105
106
public int GetInt(object key)
107
{
108
Object result=GetString(key);
109
if(result==null)
110
{
111
return 0;
112
}
113
else
114
{
115
return int.Parse( result.ToString());
116
}
117
118
}
119
public bool GetBool(object key)
120
{
121
Object result=GetString(key);
122
if(result==null)
123
{
124
return false;
125
}
126
else
127
{
128
return Convert.ToBoolean(result);
129
}
130
}
131
/**//// <summary>
132
/// read settings file
133
/// </summary>
134
public void Read()
135
{
136
list.Clear();
137
for(int i=0;i<_defaultValues.GetLength(0);i++)
138
{
139
list[_defaultValues[i,0]]=_defaultValues[i,1];
140
}
141
if (File.Exists(this.FilePath))
142
{
143
XmlTextReader reader=new XmlTextReader(this.FilePath);
144
while(reader.Read())
145
{
146
if(reader.NodeType==XmlNodeType.Element && reader.Name=="add")
147
{
148
list[reader.GetAttribute("key")]=reader.GetAttribute("value");
149
}
150
}
151
reader.Close();
152
}
153
}
154
/**//// <summary>
155
/// write settings to the .config file
156
/// </summary>
157
public void Write()
158
{
159
XmlTextWriter xmlWriter=new XmlTextWriter(this.FilePath,null);
160
161
xmlWriter.Formatting=Formatting.Indented;
162
xmlWriter.WriteStartElement("configuration");
163
xmlWriter.WriteStartElement("appSettings");
164
165
IDictionaryEnumerator enumerator=list.GetEnumerator();
166
while(enumerator.MoveNext())
167
{
168
xmlWriter.WriteStartElement("add");
169
xmlWriter.WriteStartAttribute("key",null);
170
xmlWriter.WriteString(enumerator.Key.ToString());
171
xmlWriter.WriteEndAttribute();
172
173
174
xmlWriter.WriteStartAttribute("value",null);
175
xmlWriter.WriteString(enumerator.Value.ToString());
176
xmlWriter.WriteEndAttribute();
177
178
xmlWriter.WriteEndElement();
179
180
}
181
xmlWriter.WriteEndElement();
182
xmlWriter.WriteEndElement();
183
xmlWriter.Flush();
184
xmlWriter.Close();
185
186
}
187
188
189
190
public void RestoreDefaults()
191
{
192
try
193
{
194
File.Delete(FilePath);
195
Read();
196
}
197
catch(Exception ex)
198
{
199
System.Diagnostics.Debug.Fail("删除默认配置文件时出错",ex.Message);
200
}
201
}
202
203
204
public void InitFilePath()
205
{
206
this.FilePath= Application.ExecutablePath + ".config";
207
208
}
209
}
210
211
212
public class ConfigSettingDefaults
213
{
214
public string[,] Value=new string[,]
215
{
216
{SettingKey.SQLConnectionString.ToString(),@""},
217
{SettingKey.MailUserName.ToString(),@""},
218
{SettingKey.MailPassWord.ToString(),@""},
219
{SettingKey.MailPOP3ServerAddress.ToString(),@""},
220
{SettingKey.MailSMTPServerAddress.ToString(),@""},
221
{SettingKey.MailPOP3Port.ToString(),@""},
222
{SettingKey.MailRecipientsAddress.ToString(),@""},
223
{SettingKey.MailAddress.ToString(),@""},
224
225
};
226
227
}
228
/**//// <summary>
229
/// 配置项
230
/// </summary>
231
public enum SettingKey
232
{
233
SQLConnectionString//lianjie
234
,MailUserName,MailPassWord,MailPOP3ServerAddress,MailSMTPServerAddress,MailPOP3Port,MailRecipientsAddress,MailAddress
235
}
236
237
}
238

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
