C#发现之旅:C#开发Windows Service程序(下)
2009-04-06 08:24:16 来源:WEB开发网在这个方法中,首先删除数据表SystemConfig中所有的记录,然后将所有的配置信息保存到数据表SystemConfig中。
文件系统监视服务 MyFileSystemWatcherService
类MyFileSystemWatcherService就是文件系统监视服务,它是从ServiceBase派生的,首先说明一下执行文件系统监视的功能性的过程,其代码如下
/// <summary>
/// 文件系统监视器列表
/// </summary>
private System.Collections.ArrayList myWatchers = null;
/// <summary>
/// 开始启动文件系统监视
/// </summary>
/// <returns>操作是否成功</returns>
internal bool StartFileSystemWatching()
{
myWatchers = new System.Collections.ArrayList();
MyConfig.Instance.Load();
string[] paths = MyConfig.Instance.WatchedPaths;
System.Text.StringBuilder myPathList = new StringBuilder();
if (paths != null)
{
foreach (string path in paths)
{
if (System.IO.Path.IsPathRooted(path) == false)
{
continue;
}
string BasePath = null;
string Filter = null;
if (System.IO.Directory.Exists(path))
{
BasePath = path;
Filter = "*.*";
}
else
{
BasePath = System.IO.Path.GetDirectoryName(path);
Filter = System.IO.Path.GetFileName(path);
}
if (BasePath == null)
{
continue;
}
BasePath = BasePath.Trim();
if (BasePath.ToUpper().StartsWith(System.Windows.Forms.Application.StartupPath))
{
// 不能监视程序本身所在的目录的文件系统更改
continue;
}
if (System.IO.Directory.Exists(BasePath) == false)
{
// 不能监视不存在的目录
continue;
}
if (myPathList.Length > 0)
{
myPathList.Append(";");
}
myPathList.Append(path);
System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher();
watcher.Path = BasePath;
watcher.Filter = Filter;
watcher.EnableRaisingEvents = true;
watcher.IncludeSubdirectories = false;
if (MyConfig.Instance.LogChanged)
{
watcher.Changed += delegate(object sender, System.IO.FileSystemEventArgs args)
{
WriteFileSystemLog(args.FullPath, args.ChangeType.ToString());
};
}
if (MyConfig.Instance.LogCreated)
{
watcher.Created += delegate(object sender, System.IO.FileSystemEventArgs args)
{
WriteFileSystemLog(args.FullPath, args.ChangeType.ToString());
};
}
if (MyConfig.Instance.LogDeleted)
{
watcher.Deleted += delegate(object sender, System.IO.FileSystemEventArgs args)
{
WriteFileSystemLog(args.FullPath, args.ChangeType.ToString());
};
}
if (MyConfig.Instance.LogRenamed)
{
watcher.Renamed += delegate(object sender, System.IO.RenamedEventArgs args)
{
WriteFileSystemLog(args.FullPath, args.ChangeType.ToString());
};
}
myWatchers.Add(watcher);
}//foreach
this.EventLog.WriteEntry(
"开始监视文件系统 " + myPathList.ToString(),
EventLogEntryType.Information);
}//if
return true;
}
- ››开发Android 日历教程
- ››开发学院总结 Win 8实用技巧大全
- ››开发学院原创教程:把win8的IE10放桌面上方法(非...
- ››开发者眼中的Windows Phone和Android
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››发现数据库对象的依赖关系
- ››开发一个自己的HTML在线编辑器(一)
- ››开发一个自己的HTML在线编辑器(二)
- ››开发者在App Store上赚的钱比在Android Market上多...
- ››开发者应深入学习的10个Android开源应用项目
- ››开发移动 Web Ajax 应用
- ››开发者眼中的iPhone与Android
更多精彩
赞助商链接