FileSystemWatcher类监控文件的更改状态并且实时备份文件

2022-05-07 19:09:57 浏览数 (1)

首先这是我自己在一个任务需求里面所要用到的,大致的代码如下:我把监视文件和备份文件的方法封装到一个WatcherAndBackup

类中了,但是总感觉封装的不是很好,有大牛能够指出改正之处在此留言,谢谢指点了哈!!,主要监视文件用到的类就是在sysytem.IO

里面的FileSystemWatcher,然后在一个控制台里面创建类WatcherAndBackup的实例并且运行就行

代码语言:javascript复制
 1  class WatcherAndBackup
 2     {
 3         string sourcefile = "";//源文件
 4         string targetfile = "";//目标文件
 5         string targetPath = "";//目标路径
 6         public WatcherAndBackup(string Sourcefile,string Targetfile,string TargetPath)
 7         {
 8             sourcefile = Sourcefile;targetfile = Targetfile;targetPath = TargetPath;
 9         }
10        
11         public void backup(string sourcefile,string targetfile,string targetPath)
12         {
13             try
14             {
15                 if (!Directory.Exists(targetPath))
16                 {
17                     Directory.CreateDirectory(targetPath);
18                     
19                 }
20                 File.Copy(sourcefile, targetfile, true);
21 
22             }
23             catch { }
24         }
25         #region 实时监视文件更改并且备份文件
26         public void watcherfile(string path,string file)
27         {
28             FileSystemWatcher fsw = new FileSystemWatcher(path, file);
29             fsw.Changed  = new FileSystemEventHandler(change_watcher);
30             fsw.Created  = new FileSystemEventHandler(change_watcher);
31             fsw.Deleted  = new FileSystemEventHandler(change_watcher);
32             fsw.Renamed  = new RenamedEventHandler(rename_watcher);
33             fsw.EnableRaisingEvents = true;
34             Console.WriteLine("开始监视。。。。");
35             fsw.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName
36                 | NotifyFilters.LastAccess | NotifyFilters.Security | NotifyFilters.Size | NotifyFilters.LastWrite;
37             fsw.IncludeSubdirectories = true;
38         }
39         public void change_watcher(object sender,FileSystemEventArgs e)
40         {
41            
42             var wacher = sender as FileSystemWatcher;
43             wacher.EnableRaisingEvents = false;
44             
45             if (e.ChangeType==WatcherChangeTypes.Changed)
46             {
47                 Console.WriteLine("正在备份。。。");
48                     backup(sourcefile,targetfile,targetPath);
49                     Console.WriteLine("备份成功。。。");
50                                
51             }
52             else if (e.ChangeType==WatcherChangeTypes.Created)
53             {
54                 Console.WriteLine("{0},{1},{2}", e.ChangeType, e.FullPath, e.Name);
55                 return;
56             }
57             else if (e.ChangeType==WatcherChangeTypes.Deleted)
58             {
59                 Console.WriteLine("{0},{1},{2}", e.ChangeType, e.FullPath, e.Name);
60                 return;
61             }
62             wacher.EnableRaisingEvents = true;
63         }
64         public void rename_watcher(object sender,RenamedEventArgs e)
65         {
66             Console.WriteLine("{0},{1},{2}", e.ChangeType, e.FullPath, e.Name);
67         }
68         #endregion
69 
70     }
代码语言:javascript复制
 static void Main(string[] args)
        {
            WatcherAndBackup bk = new WatcherAndBackup(@"D:ggconfig.xml", @"D:ggbackupconfig.xml", @"D:ggbackup");
            bk.watcherfile(@"D:gg", "config.xml");//监视的文件为D:ggconfig.xml
            Console.Read();         
        }

 在这里解释一下:实例类WatcherAndBackup时分别要写下backup方法的三个参数:sourcefile、targefile、targePath,也就是备份方法的源文件、目标文件、目标文件的目录,然后在change_watcher方法当中为什么会有这几局代码:

代码语言:javascript复制
var wacher=sender as FileSystemWatcher;
wacher.EnableRaisingEvents=false;

然后在方法后面:

代码语言:javascript复制
wacher.EnableRaisingEvents=true;

其实如果不加入这几句代码会出现当监控到文件修改时会触发两次changed方法,这个修改方法是我在网上找到的一个修改方法

好了,基本也说完了。。。有什么不正确的地方请各位大牛指正,本就打着学习的态度写下的。。嘿嘿!!

0 人点赞