FileSystemWatcher类监测文件夹是否发生改变

static void MonitorGiftFile()
{
        //首先实例化一个对象
        FileSystemWatcher watcher = new FileSystemWatcher();
        //设置监听路径
        watcher.Path = FreeRoomGiftRoot;
        //只监听这一个文件,当礼品目录修改完毕后对此文件进行修改,由此触发监听事件   
        watcher.Filter = "Monitor.txt";     
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        //启用监听,如果不设置则不会触发事件
        watcher.EnableRaisingEvents = true;
        //设置监听触发事件
        watcher.Changed += new FileSystemEventHandler(watcher_Changed);
}

 

        /// <summary>
        /// 监听到目标发生改变后所要执行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

原文地址:https://www.cnblogs.com/mahongbo/p/1403193.html