自动加载修改过的配置文件。

核心类如下:
1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.Text;
5
using Microsoft.SharePoint.Administration;
6
using Microsoft.SharePoint;
7
using System.IO;
8
using System.Configuration;
9
10
using System.Security.Permissions;
11
using System.Xml;
12
using System.Diagnostics;
13
14
using System.Runtime.InteropServices;
15
using System.Runtime.CompilerServices;
16
namespace TaskJob
17

{
18
public class TaskConfig : IConfigurationSectionHandler
19
{
20
IConfigurationSectionHandler Members#region IConfigurationSectionHandler Members
21
public static object ReadConfig(string FileName, string SectionName)
22
{
23
XmlReader reader = null;
24
try
25
{
26
XmlDocument doc = new XmlDocument();
27
reader = new XmlTextReader(FileName);
28
doc.Load(reader);
29
XmlNode node = doc.SelectSingleNode("configuration/" + SectionName);
30
return ReadConfig(node);
31
}
32
catch
33
{
34
throw;
35
}
36
finally
37
{
38
if (reader != null)
39
reader.Close();
40
}
41
return null;
42
}
43
public static object ReadConfig(System.Xml.XmlNode section)
44
{
45
List<TaskItem> list = new List<TaskItem>();
46
TaskItem ti = null;
47
foreach (System.Xml.XmlNode node in section.ChildNodes)
48
{
49
try
50
{
51
ti = new TaskItem();
52
ti.JobName = node.Attributes["JobName"].Value;
53
ti.JobTime = int.Parse(node.Attributes["JobTime"].Value);
54
//ti.Task = (ITask)new System.Reflection.Assembly.CreateInstance(node.Attributes["Task"].Value);
55
string str = node.Attributes["Type"].Value.ToString();
56
ti.Task = (ITask)Type.GetType(str).Assembly.CreateInstance(str.Split(',')[0]);
57
Console.WriteLine("JobName={0},JobTime={1},Type={2}", ti.JobName, ti.JobTime, str);
58
}
59
catch(Exception ee)
60
{
61
EventLogHandle.WriteException(ee);
62
continue;
63
}
64
list.Add(ti);
65
}
66
return list;
67
}
68
public object Create(object parent, object configContext, System.Xml.XmlNode section)
69
{
70
return ReadConfig(section);
71
}
72
73
#endregion
74
}
75
public interface ITask
76
{
77
void Execute(Guid SiteID);
78
}
79
public class TaskItem
80
{
81
public string JobName = "";
82
public string SiteName = "";
83
public int JobTime = 5;
84
public int CurrentTime = 0;
85
public ITask Task = null;
86
}
87
public class Task : Microsoft.SharePoint.Administration.SPJobDefinition
88
{
89
public Task()
90
: base()
91
{
92
ReadConfig();
93
}
94
95
public Task(string jobName, SPService service, SPServer server, SPJobLockType targetType)
96
: base(jobName, service, server, targetType)
97
{
98
ReadConfig();
99
}
100
101
public Task(string jobName, SPWebApplication webApplication)
102
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
103
{
104
this.Title = "Task Logger";
105
ReadConfig();
106
}
107
static Hashtable ob = new Hashtable();
108
static List<TaskItem> list = new List<TaskItem>();
109
static Guid FeatureID = new Guid("1F481C17-4FDA-4919-A64A-EAE5C1301B4B");
110
private string basepath =AppDomain.CurrentDomain.BaseDirectory; //@"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\bin";
111
private string configName = "OwsTimer.exe.config";//"Task.dll.config";
112
/**//// <summary>
113
/// 读取配置信息:
114
/// 格式:
115
/// <Jobs>
116
///<Job JobName="JobName" Type="" JobTime="2" SiteName=""/>
117
///</Jobs>
118
/// </summary>
119
private void ReadConfig()
120
{
121
list = TaskConfig.ReadConfig(basepath + "\\"+configName, "Jobs") as List<TaskItem>;
122
StartWatcher(basepath, configName, true);
123
}
124
static FileSystemWatcher watcher = new FileSystemWatcher();
125
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
126
private static void StartWatcher(string Filepath, string strFilter, bool flag)
127
{
128
129
try
130
{
131
if (flag == true)
132
{
133
EventLogHandle.WriteEvent(new string[]
{ "启动文件监视器开始……" });
134
watcher.Filter = strFilter;
135
watcher.Path = Filepath;
136
watcher.NotifyFilter = NotifyFilters.LastWrite;
137
watcher.Changed += new FileSystemEventHandler(OnChanged);
138
EventLogHandle.WriteEvent(new string[]
{ "文件监视器启动成功。" });
139
}
140
else
141
{
142
EventLogHandle.WriteEvent(new string[]
{ "关闭文件监视器开始……" });
143
watcher.Changed -= new FileSystemEventHandler(OnChanged);
144
EventLogHandle.WriteEvent(new string[]
{ "文件监视器关闭完成" });
145
}
146
watcher.EnableRaisingEvents = flag;
147
}
148
catch (Exception ee)
149
{
150
EventLogHandle.WriteException(ee);
151
}
152
}
153
private static void OnChanged(object source, FileSystemEventArgs e)
154
{
155
System.Threading.Thread.Sleep(5000);
156
lock (ob.SyncRoot)
157
{
158
list.Clear();
159
try
160
{
161
list = TaskConfig.ReadConfig(e.FullPath, "Jobs") as List<TaskItem>;
162
}
163
catch (Exception ee)
164
{
165
list = new List<TaskItem>();
166
EventLogHandle.WriteException(ee);
167
}
168
}
169
}
170
/**//// <summary>
171
/// 执行多个任务
172
/// </summary>
173
/// <param name="contentDbId"></param>
174
public override void Execute(Guid contentDbId)
175
{
176
lock (ob.SyncRoot)
177
{
178
foreach (TaskItem ti in list)
179
{
180
try
181
{
182
ti.CurrentTime++;
183
if (ti.CurrentTime == ti.JobTime)
184
{
185
//foreach (SPSite site in WebApplication.Sites)
186
//{
187
// foreach (SPFeature fea in site.Features)
188
// {
189
// if (fea.Definition.SolutionId == FeatureID && fea.Definition.Status == SPObjectStatus.Online)
190
// {
191
// try
192
// {
193
// ti.Task.Execute(site.ID);
194
// }
195
// catch(Exception ee)
196
// {
197
// WriteEvent(site.Url, ee.ToString());
198
// EventLogHandle.WriteException(ee);
199
// }
200
// }
201
// }
202
//}
203
ti.Task.Execute(contentDbId);
204
205
}
206
}
207
catch (Exception eee)
208
{
209
WriteEvent(ti.JobName, eee.ToString());
210
EventLogHandle.WriteException(eee);
211
}
212
finally
213
{
214
if (ti.CurrentTime == ti.JobTime)
215
{
216
ti.CurrentTime = 0;
217
}
218
}
219
}
220
}
221
/**///// get a reference to the current site collection's content database
222
//SPWebApplication webApplication = this.Parent as SPWebApplication;
223
//SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
224
225
/**///// get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database
226
//SPList taskList = contentDb.Sites[0].RootWeb.Lists["Tasks"];
227
228
/**///// create a new task, set the Title to the current day/time, and update the item
229
//SPListItem newTask = taskList.Items.Add();
230
//newTask["Title"] = DateTime.Now.ToString();
231
//newTask.Update();
232
//SPSecurity.RunWithElevatedPrivileges(delegate()
233
//{
234
// try
235
// {
236
// using (StreamWriter sw = new StreamWriter("C:\\time.txt", false, System.Text.Encoding.UTF8))
237
// {
238
// sw.WriteLine("这是第{0}次写入,当前时间是:{1}", nCount.ToString(), DateTime.Now.ToLongDateString());
239
// }
240
// }
241
// catch
242
// {
243
// }
244
//});
245
}
246
static void WriteEvent(string SiteName, string msg)
247
{
248
SPSecurity.RunWithElevatedPrivileges(delegate()
249
{
250
try
251
{
252
using (StreamWriter sw = new StreamWriter("C:\\time.txt", true, System.Text.Encoding.UTF8))
253
{
254
sw.WriteLine("\n时间:{2}\n站点名称:{0}\n日志信息:{1}", SiteName, msg,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
255
}
256
}
257
catch
258
{
259
}
260
});
261
}
262
}
263
264
class EventLogHandle
265
{
266
static EventLog _eventLog = null;
267
static EventLogHandle()
268
{
269
_eventLog = new EventLog("Application", ".", "SPTaskProvider");
270
}
271
public static void WriteException(Exception e)
272
{
273
_eventLog.WriteEntry(string.Concat(e.Message, Environment.NewLine, Environment.NewLine, e.StackTrace), EventLogEntryType.Error);
274
}
275
public static void WriteEvent(string [] strArray)
276
{
277
_eventLog.WriteEntry(string.Concat(strArray), EventLogEntryType.Information);
278
}
279
public static void WriteWarn(string[] strArray)
280
{
281
_eventLog.WriteEntry(string.Concat(strArray), EventLogEntryType.Warning);
282
}
283
}
284
}
285

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

260

261

262

263

264

265



266

267

268



269

270

271

272



273

274

275

276



277

278

279

280



281

282

283

284

285

调试用控制台源
码:


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



1

2

3

4

5

6

7

8

9

10

11

完成