FileSystemWatcher.cs

本文介绍了一个使用C#实现的文件系统监控程序,通过FileSystemWatcher类监控指定目录下的.txt文件变动,包括创建、修改、删除及重命名等事件。
/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 * Version: 1
 */
using System;
using System.IO;

namespace Client.Chapter_11___File_and_Streams
{
    public class Test
    {
        public static void Main(string[] args)
        {
             FileSystemWatcher watcher = new FileSystemWatcher();
             watcher.Path = @"c:\Test";
             watcher.NotifyFilter  =
                                                    NotifyFilters.LastAccess |
                                                    NotifyFilters.LastWrite |
                                                    NotifyFilters.FileName |
                                                    NotifyFilters.DirectoryName;
             watcher.Filter = "*.txt";
             watcher.Changed += new FileSystemEventHandler(OnChanged);
             watcher.Created += new FileSystemEventHandler(OnChanged);
             watcher.Deleted += new FileSystemEventHandler(OnChanged);
             watcher.Renamed += new RenamedEventHandler(OnRenamed);
             watcher.EnableRaisingEvents = true;
                         
        }
        
        public static void OnChanged(object source, FileSystemEventArgs e)
        {
               Console.WriteLine("Event Fired");
        }
        public static void OnRenamed(object source, RenamedEventArgs e)
        {
            Console.WriteLine("Event Fired");
        }
    
    }
}

转载于:https://www.cnblogs.com/shihao/archive/2012/05/15/2501876.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值