首先这是我自己在一个任务需求里面所要用到的,大致的代码如下:我把监视文件和备份文件的方法封装到一个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. 实验:企业级分布式存储应用与实战-mogilefs实现

    实验:企业级分布式存储应用与实战-mogilefs实现 (1)安装mogilefs 1.创建一个存放安装mogilefs所需的软件包的目录 cd /app/ mkdir mogilefs cd mog ...

  2. Linux系统bash shell之历史命令

    1.相关变量: HISTSIZE: 定义命令历史记录的条数 HISTFILE: 定义命令储存的文件,一般是 ~/.bash_history HISTFILESIZE: 定义了历史文件记录历史的条数 H ...

  3. var a=function(){...}与function a(){...}的区别

    var a = function(){...}在js预加载时按变量处理,即预加载定义,不加载赋值. function a(){...}即加载定义,而且赋值. 例如:a(); //正常执行a()函数; ...

  4. ThreadLocal终极源码剖析

    目录一.ThreadLocal1.1 源码注释1.2 源码剖析      散列算法-魔数0x61c88647      set操作    get操作    remove操作1.3 功能测试1.4 应用 ...

  5. CCF-201412-2-Z字形扫描

    问题描述 试题编号: 201412-2 试题名称: Z字形扫描 时间限制: 2.0s 内存限制: 256.0MB 问题描述: 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zi ...

  6. 关于mui header在手机上运行丢失问题

    并不需要换header, 只需要把引用的例子自带的CSS文件 app.css.里的两个样式:.mui-plus.mui-android header.mui-bar {display: none;}. ...

  7. ofBiz-groovy-freemarker

    ofBiz-groovy-freemarker根据浏览器的地址不同进入不同的页面 第一步:(2选一)创建groovy文件,或者java文件.在文件中定义变量 要放在 request.setAttrib ...

  8. PHP开发中需要注意几点事项,新手少走弯路必备知识

    这篇文章主要介绍了PHP开发需要注意的几点事项总结,非常详细,需要的朋友可以参考下.新手多看看避免走弯路. 1.使用内嵌的HTML代码,而不是PHP的echo语句. 因为PHP是一门嵌入式Web编程语 ...

  9. 》》初识移动端--rem

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...

  10. Cocos2d-x 3.0 Android改动APK名、更改图标、改动屏幕方向、改动版本,一些须要注意的问题

    非常多新手程序员做出一个游戏后,编译成apk安装在手机上.却发现安装程序名和游戏图标都是Cocos2dx默认的,并且默认屏幕方向是横向.那么须要怎么才干改动为自己想要的呢? 打开你创建的project ...