WEB开发网
开发学院软件开发C语言 C#发现之旅:C#开发Windows Service程序(下) 阅读

C#发现之旅:C#开发Windows Service程序(下)

 2009-04-06 08:24:16 来源:WEB开发网   
核心提示: 在这个方法中,首先删除数据表SystemConfig中所有的记录,C#发现之旅:C#开发Windows Service程序(下)(5),然后将所有的配置信息保存到数据表SystemConfig中,文件系统监视服务 MyFileSystemWatcherService类MyFileSyste

在这个方法中,首先删除数据表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;
}

上一页  1 2 3 4 5 6 7 8 9 10  下一页

Tags:发现 之旅 开发

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接