zoukankan      html  css  js  c++  java
  • .net 监听本地目录

    捕获事件FileSystemWatcher

    例子:

    /// <summary>
            /// 监听目录
            /// </summary>
            /// <param name="path">目录</param>
            /// <param name="filter">过滤器(文件类型)</param>
            /// <param name="includeSubDirs">是否递归子目录</param>
            static void Watch(string path, string filter, bool includeSubDirs)
            {
                using (var watcher=new FileSystemWatcher(path,filter))
                {
                    watcher.Created += Watcher_Changed;
                    watcher.Deleted += Watcher_Changed;
                    watcher.Changed += Watcher_Changed;
                    watcher.Renamed += Watcher_Renamed;  
                     
                    watcher.Error += Watcher_Error;
                    watcher.IncludeSubdirectories = includeSubDirs;
                    watcher.EnableRaisingEvents = true;
    
                    Console.WriteLine("Listening for - pree<Enter> to end");
                    Console.ReadLine();
                }
               
            }
    
            private static void Watcher_Renamed(object sender, RenamedEventArgs e)
            {
                Console.WriteLine($"Renamed:{e.OldFullPath} => {e.FullPath}");
            }
    
            private static void Watcher_Error(object sender, ErrorEventArgs e)
            {
                Console.WriteLine($"Error:+{e.GetException().Message}");
            }
    
            private static void Watcher_Changed(object sender, FileSystemEventArgs e)
            {
                Console.WriteLine($"File {e.FullPath} has been {e.ChangeType}");
            }
    
    static void Main(string[] args)
            {
    
                Watch(@"C:\Users\Administrator\Desktop\IO", "*.*", true);
    }

  • 相关阅读:
    sqlalchemy-数据目录集合整合
    算法-lowb三人组
    ipython ---matplotlib:绘图和可视化
    ipython --pandas
    ipython --之Numpy
    字符编码
    Markdown——入门使用
    python集合操作和内置方法
    python字典操作和内置方法
    python元祖操作和内置方法
  • 原文地址:https://www.cnblogs.com/Zingu/p/15696351.html
Copyright © 2011-2022 走看看