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

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

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

  class WatcherAndBackup
{
string sourcefile = "";//源文件
string targetfile = "";//目标文件
string targetPath = "";//目标路径
public WatcherAndBackup(string Sourcefile,string Targetfile,string TargetPath)
{
sourcefile = Sourcefile;targetfile = Targetfile;targetPath = TargetPath;
} public void backup(string sourcefile,string targetfile,string targetPath)
{
try
{
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath); }
File.Copy(sourcefile, targetfile, true); }
catch { }
}
#region 实时监视文件更改并且备份文件
public void watcherfile(string path,string file)
{
FileSystemWatcher fsw = new FileSystemWatcher(path, file);
fsw.Changed += new FileSystemEventHandler(change_watcher);
fsw.Created += new FileSystemEventHandler(change_watcher);
fsw.Deleted += new FileSystemEventHandler(change_watcher);
fsw.Renamed += new RenamedEventHandler(rename_watcher);
fsw.EnableRaisingEvents = true;
Console.WriteLine("开始监视。。。。");
fsw.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName
| NotifyFilters.LastAccess | NotifyFilters.Security | NotifyFilters.Size | NotifyFilters.LastWrite;
fsw.IncludeSubdirectories = true;
}
public void change_watcher(object sender,FileSystemEventArgs e)
{ var wacher = sender as FileSystemWatcher;
wacher.EnableRaisingEvents = false; if (e.ChangeType==WatcherChangeTypes.Changed)
{
Console.WriteLine("正在备份。。。");
backup(sourcefile,targetfile,targetPath);
Console.WriteLine("备份成功。。。"); }
else if (e.ChangeType==WatcherChangeTypes.Created)
{
Console.WriteLine("{0},{1},{2}", e.ChangeType, e.FullPath, e.Name);
return;
}
else if (e.ChangeType==WatcherChangeTypes.Deleted)
{
Console.WriteLine("{0},{1},{2}", e.ChangeType, e.FullPath, e.Name);
return;
}
wacher.EnableRaisingEvents = true;
}
public void rename_watcher(object sender,RenamedEventArgs e)
{
Console.WriteLine("{0},{1},{2}", e.ChangeType, e.FullPath, e.Name);
}
#endregion }
 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方法,这个修改方法是我在网上找到的一个修改方法

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


            
 
 
 

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

  1. C# FileSystemWatcher 在监控文件夹和文件时的用法

    概述 最近学习FileSystemWatcher的用法,它主要是监控一个文件夹,当文件夹内的文件要是有更改就要记录下来,我就整理下我对FileSystemWatcher 的理解和用法. FileSys ...

  2. C#使用FileSystemWatcher来监控指定文件夹,并使用TCP/IP协议通过Socket发送到另外指定文件夹

    项目需求: 局域网内有两台电脑,电脑A(Windows系统)主要是负责接收一些文件(远程桌面粘贴.FTP上传.文件夹共享等方式),希望能在A接收文件后自动传输到电脑B(Windows系统)来做一个备份 ...

  3. 使用FileSystemWatcher监控文件夹及文件

    引言 这一周主要精力集中学习一个同事开发的本地文件搜索项目上,其中客户端添加共享文件时主要是使用FileSystemWatcher 监控文件,并在各种事件发生时向服务器发送消息. 解决方法 FileS ...

  4. 工具类commons-io的Tailer用来监控文件

    一.前言:在Linux下有使用tail命令 在Commons-io中也提供这种方法 二.他采用的是线程方式来监控文件内容的变化 1.Tailer类(采用线程的方式进行文件的内容变法) 2.Tailer ...

  5. 【.Net 学习系列】-- FileSystemWatcher 监控文件夹新生成文件,并在确认文件没有被其他程序占用后将其移动到指定文件夹

    监控文件夹测试程序: using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...

  6. [转帖]Linux下inotify监控文件夹状态,发生变化后触发rsync同步

    Linux下inotify监控文件夹状态,发生变化后触发rsync同步 https://www.cnblogs.com/fjping0606/p/6114123.html 1.安装工具--inotif ...

  7. Windows系统中监控文件复制操作的几种方式

    http://blog.sina.com.cn/s/blog_4596beaa0100lp4y.html 1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得 ...

  8. C#监控文件夹变化

    当需要监控某一文件,FileSystemWatcher类提供了Created, Deleted,Rename等事件. 就拿FileSystemWatcher的Created事件来说,该事件类型是Fil ...

  9. Python 的 pyinotify 模块 监控文件夹和文件的变动

    官方参考: https://github.com/seb-m/pyinotify/wiki/Events-types https://github.com/seb-m/pyinotify/wiki/I ...

随机推荐

  1. LKD: Chapter 9 An Introduction to Kernel Synchronization

    This chapter introduces some conception about kernel synchronization generally. Critical Regions: Co ...

  2. better-scroll 实现tab栏目滑动当前高亮始终在可视区

    https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/#better-scroll better-scroll文档地址 如图 ,是我们要实现的 ...

  3. 学python3的书

    <Python Cookbook>3rd Edition http://python3-cookbook.readthedocs.io/zh_CN/latest/copyright.htm ...

  4. ViewPager +无限轮播+滑动速度修改+指示小点

    养成习惯,做过代码记录总结. ViewPager 使用记录 1. ViewPage 位于V4包. 2.主要用来做banner轮播. 3.原理:适配器重用提高效率,与listview等一个原理. 下面记 ...

  5. Unity灯光烘焙

    Unity3D烘焙技术 一.Light灯光场景烘焙1.理论理解:(1)烘焙背景:在一个场景中,由于灯光组件起到实时渲染的效果,并直接与计算机硬件GPU渲染器进行交互作用,因此对计算机显卡性能不良,以至 ...

  6. Java第三季

    1.异常简介: (1) Java中的所有不正常类都继承于Throwable类.Throwable主要包括两个大类,一个是Error类,另一个是 Exception类: (2)其中Error类中包括虚拟 ...

  7. Java数据结构和算法(五)——队列

    前面一篇博客我们讲解了并不像数组一样完全作为存储数据功能,而是作为构思算法的辅助工具的数据结构——栈,本篇博客我们介绍另外一个这样的工具——队列.栈是后进先出,而队列刚好相反,是先进先出. 1.队列的 ...

  8. windows 下更新 npm 和 node

    原文链接 公司的新项目要启动了,需要使用 Angular 4.0,并且使用 webpack 工具进行打包.所以就需要安装 node.node 的安装很简单,在 node 的官网 nodejs.org  ...

  9. Webstorm 快捷键大全 整理收录

    "工欲善其事,必先利其器" 作为一名开发人员,好用的工具能让你效率更高,剩下的时间用来偷懒吹牛逼吧... 以下整理Webstorm快捷键大全   Windows版本 注释(// 或 ...

  10. Mac中配置nvm

    1.安装 nvm curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash 安装成功默认将 ...