C# FileSystemWatcher

摘要:
staticvoidMain(string[]args){Tasktask=Task.Run(()=˃{CreateRndTextFiles();});stringpath=目录。获取当前目录();FileSystemWatchDemo(路径,“*”,true);安慰ReadLine();}私有站点创建
 static void Main(string[] args)
        {
            Task task = Task.Run(() => 
            { 
                CreateRndTextFiles();
            });
           
            string path = Directory.GetCurrentDirectory();
            FileSystemWatchDemo(path, "*", true);
           
            Console.ReadLine();
        }

        private static void CreateRndTextFiles()
        {
            for(int i=0;i<10000;i++)
            {
                Thread.Sleep(10);
                File.Create(Guid.NewGuid().ToString() + ".txt");                
            }            
        }

        static void FileSystemWatchDemo(string path,string filter,bool includeSubDirs)
        {
            using(FileSystemWatcher fsw=new FileSystemWatcher(path))
            {
                fsw.Created += FileCreatedChangedDeteled;
                fsw.Changed += FileCreatedChangedDeteled;
                fsw.Deleted += FileCreatedChangedDeteled;
                fsw.Renamed += FswRenamed;
                fsw.Error += FswError;
                fsw.IncludeSubdirectories = includeSubDirs;
                fsw.EnableRaisingEvents = true;
                Console.WriteLine("Listening for events-press <enter> to end");
                Console.ReadLine();
            }
        }

        private static void FswError(object sender, ErrorEventArgs e)
        {
            Console.WriteLine($"Error:{e.GetException().Message}");
        }

        private static void FswRenamed(object sender, RenamedEventArgs e)
        {
            Console.WriteLine($"Renamed:{e.OldFullPath}->{e.FullPath}");
        }

        private static void FileCreatedChangedDeteled(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine($"File {e.FullPath} has been {e.ChangeType}");
        }

免责声明:文章转载自《C# FileSystemWatcher》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇浏览器的统一指针事件:Pointer EventOracle sqlplus prelim 参数介绍下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

guava API整理

1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API Cache API 2,为神马选择瓜娃? 瓜娃是java API蛋糕上的冰激凌(精华) 高效设计良好的API. 被google的开发者设计,实现和使用。...

MySqlHelper、CacheHelper

MySqlHelper代码: using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using M...

Guava LoadingCache不能缓存null值

测试的时候发现项目中的LoadingCache没有刷新,但是明明调用了refresh方法了。后来发现LoadingCache是不支持缓存null值的,如果load回调方法返回null,则在get的时候会抛出异常。 通过几个例子开看这个问题: public void test_loadNull() { LoadingCache<String,...

Unity3D脚本中文系列教程(七)

http://dong2008hong.blog.163.com/blog/static/4696882720140311445677/?suggestedreading&wumii Unity3D脚本中文系列教程(六) 类方法◆ static function BeginGroup(GroupName : string) : void ◆ sta...

excel数据批量导入

1.  html           <form action="@Url.Action("UpLoadFile")" enctype="multipart/form-data" method="post">                                  <td>                         ...

Java 权限框架 Shiro 实战一:理论基础

Apache Shiro 官网地址:http://shiro.apache.org/ Apache Shiro is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and sessio...