zoukankan      html  css  js  c++  java
  • 监视文件夹的变更

    代码
    using System;
    using System.IO;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Watch();
    }
    static DateTime now = DateTime.Now;
    static string fileName = string.Empty;

    private static void Watch()
    {
    FileSystemWatcher myWatcher
    = new FileSystemWatcher("E:\\drivers");
    myWatcher.Filter
    = "*.*";
    myWatcher.EnableRaisingEvents
    = true;
    myWatcher.Changed
    += new FileSystemEventHandler(myWatcher_Changed);
    myWatcher.Created
    += new FileSystemEventHandler(myWatcher_Created);
    myWatcher.Deleted
    += new FileSystemEventHandler(myWatcher_Deleted);
    myWatcher.Renamed
    += new RenamedEventHandler(myWatcher_Renamed);
    Console.Read();
    }

    static void myWatcher_Renamed(object sender, RenamedEventArgs e)
    {
    if ((DateTime.Now - now).TotalMilliseconds > 100 || fileName != e.Name)
    {
    Console.WriteLine(
    string.Format("{0} --重命名-- {1}", e.OldName, e.Name));
    fileName
    = e.Name;
    now
    = DateTime.Now;
    }
    }

    static void myWatcher_Deleted(object sender, FileSystemEventArgs e)
    {
    if ((DateTime.Now - now).TotalMilliseconds > 100 || fileName != e.Name)
    {
    Console.WriteLine(
    "删除");
    fileName
    = e.Name;
    now
    = DateTime.Now;
    }
    }

    static void myWatcher_Created(object sender, FileSystemEventArgs e)
    {
    if ((DateTime.Now - now).TotalMilliseconds > 100 || fileName != e.Name)
    {
    Console.WriteLine(
    "创建");
    fileName
    = e.Name;
    now
    = DateTime.Now;
    }
    }

    static void myWatcher_Changed(object sender, FileSystemEventArgs e)
    {
    if ((DateTime.Now - now).TotalMilliseconds > 100 || fileName != e.Name)
    {
    Console.WriteLine(
    "内容改变");
    fileName
    = e.Name;
    now
    = DateTime.Now;
    }
    }
    }
    }

    转载:http://www.cnblogs.com/magic_evan/archive/2011/01/22/1941864.html



    返回导读目录,阅读更多随笔



    分割线,以下为博客签名:

    软件臭虫情未了
    • 编码一分钟
    • 测试十年功


    随笔如有错误或不恰当之处、为希望不误导他人,望大侠们给予批评指正。

  • 相关阅读:
    Icinga使用总结
    Linux入门:usermod
    JMeter入门(01)概念和样例
    英语日常词汇:living-room、dining-room vs dining hall
    安装 docker-compose
    CentOS7下安装python-pip
    JMeter入门(03)多台JMeter联合测试
    jmeter入门(02)测试报告各项指标含义
    istio入门(01)istio的优势在哪里?
    Docker学习笔记
  • 原文地址:https://www.cnblogs.com/08shiyan/p/1943186.html
Copyright © 2011-2022 走看看