文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>监视文件夹的文件变更

监视文件夹的文件变更

时间:2011-01-22  来源:magic_evan

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;
            }
        }
    }
}

 

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载