码迷,mamicode.com
首页 > 其他好文 > 详细

使用FileSystemWatcher捕获系统文件状态

时间:2014-12-10 12:28:35      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   使用   sp   for   on   

bubuko.com,布布扣

源代码:

using System;

using System.Collections.Generic;

using System.Linq; using System.Text;

using System.Threading.Tasks; using System.IO;

namespace ConsoleApplication138

{     class Program    

{         static void Main(string[] args)      

   {

            Watch(@"c:\t", "*.txt", true);     

    }       

  static void Watch(string path,string filter,bool includesubdirs)   

      {             using (FileSystemWatcher watcher=new FileSystemWatcher (path ,filter ))    

         {                 watcher.Created += watcher_createchangeddeleted;           

      watcher.Changed += watcher_createchangeddeleted;               

  watcher .Deleted +=watcher_createchangeddeleted;              

   watcher .Renamed +=watcher_Renamed;                

watcher .Error +=watcher_Error;               

  watcher.IncludeSubdirectories = includesubdirs;    

             watcher.EnableRaisingEvents = true;            

     Console.WriteLine("LIstening for events -press <enter> to end");  

               Console.Read();             }         }       

  static void watcher_createchangeddeleted(object sender,FileSystemEventArgs e)  

       {             Console.WriteLine("File :{0} has been {1}", e.FullPath, e.ChangeType);         }  

       static void watcher_Renamed(object sender,RenamedEventArgs e)      

   {             Console.WriteLine("renamed:{0}->{1}", e.OldFullPath, e.FullPath);

        }         static void watcher_Error(object sender,ErrorEventArgs e)        

{             Console.WriteLine("error: " + e.GetException().Message);         }

    } }

使用FileSystemWatcher捕获系统文件状态

标签:des   blog   http   io   ar   使用   sp   for   on   

原文地址:http://www.cnblogs.com/fandong90/p/4154982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!