
FileSystemWatcher类监控文件的更改状态并且实时备份文件
发布日期:2021-05-09 06:18:03
浏览次数:21
分类:原创文章
本文共 3696 字,大约阅读时间需要 12 分钟。
首先这是我自己在一个任务需求里面所要用到的,大致的代码如下:我把监视文件和备份文件的方法封装到一个WatcherAndBackup
类中了,但是总感觉封装的不是很好,有大牛能够指出改正之处在此留言,谢谢指点了哈!!,主要监视文件用到的类就是在sysytem.IO
里面的FileSystemWatcher,然后在一个控制台里面创建类WatcherAndBackup的实例并且运行就行
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 try14 {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.FileName36 | 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 #endregion69 70 }
static void Main(string[] args) { WatcherAndBackup bk = new WatcherAndBackup(@"D:\gg\config.xml", @"D:\gg\backup\config.xml", @"D:\gg\backup"); bk.watcherfile(@"D:\gg", "config.xml");//监视的文件为D:\gg\config.xml Console.Read(); }
在这里解释一下:实例类WatcherAndBackup时分别要写下backup方法的三个参数:sourcefile、targefile、targePath,也就是备份方法的源文件、目标文件、目标文件的目录,然后在change_watcher方法当中为什么会有这几局代码:
var wacher=sender as FileSystemWatcher;
wacher.EnableRaisingEvents=false;
然后在方法后面:
wacher.EnableRaisingEvents=true;
其实如果不加入这几句代码会出现当监控到文件修改时会触发两次changed方法,这个修改方法是我在网上找到的一个修改方法
好了,基本也说完了。。。有什么不正确的地方请各位大牛指正,本就打着学习的态度写下的。。嘿嘿!!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年05月10日 14时37分49秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
legend2---开发日志3(thinkphp的入口目录是public的体现是什么)
2023-01-31
legoblock秀上限
2023-01-31
LeNet介绍-ChatGPT4o作答
2023-01-31
LeNet剪枝
2023-01-31
Lenovo E47A Ubuntu闪屏解决办法
2023-01-31
Leopard系统装好后不能从硬盘引导的朋友看过来
2023-01-31
LESS 中的变量有什么作用?如何声明和使用变量?
2023-01-31
Less 日常用法
2023-01-31
Lettuce 移动框架 for Romantic
2023-01-31
let、const、var的四点区别( 代码示例 )
2023-01-31
LexPredict法律词典项目教程
2023-01-31
LFS最终幻想
2023-01-31
lftp命令详解
2023-01-31
lib/libstdc++.so.6: version `GLIBCXX_3.4.30‘ not found (required by /lib/x86_64-linux-gnu/libLLVM-15
2023-01-31
Libevent 事件管理和添加事件
2023-01-31
libevent-简单的定时器
2023-01-31
libevent在windows下使用步骤详解
2023-01-31
libgdx的菜单配置,以及json文件的结构
2023-01-31
libiconv字符集转换库在C#中的使用
2023-01-31