使用FileSystemWatcher捕获系统文件状态_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 使用FileSystemWatcher捕获系统文件状态

使用FileSystemWatcher捕获系统文件状态

 2014/12/10 13:11:38  Company  程序员俱乐部  我要评论(0)
  • 摘要:源代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;namespaceConsoleApplication138{classProgram{staticvoidMain(string[]args){Watch(@"c:\t","*.txt",true);}staticvoidWatch
  • 标签:file system files 使用 文件

源代码:

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

    } }

发表评论
用户名: 匿名