zoukankan      html  css  js  c++  java
  • 使用FileSystemWatcher捕获系统文件状态

    源代码:

    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: ", "*.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);         }

        } }

    歡迎分享,謝謝!
  • 相关阅读:
    连接到 ASP.NET 数据库
    ASP.NET数据库操作代码小结:SQL Server篇
    无法远程连接 SQL Server 2008
    使用 ADO.NET 来创建、读取、更新和删除数据
    User表引发的"FROM子句语法错误"
    传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确
    ANSI,Unicode,UTF8网页编码的区别【转】
    ASP.NET 数据库原理【转】
    Eval()时间格式化
    Eclipse下无法自动编译,或者WEBINF/classes目录下没文件,编译失败的解决办法
  • 原文地址:https://www.cnblogs.com/fandong90/p/4154982.html
Copyright © 2011-2022 走看看