C# FileSystemWatcher监控新生成的文件

FileSystemWatcher 可以监控到文件某路径下是否有新的文件产生,并且可以获取到最新文件的基本信息。
一、单条路径监控

   main(){
   				    string pathArray = @"D:\Program Files (x86)\Microsoft Office";  
                    FileSystemWatcher watch = new FileSystemWatcher();  // 实例化FileSystemWatcher对象
                    watch.Path = pathArray;   //监控的路径
                    watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName |                          
                    NotifyFilters.DirectoryName;
                    watch.Filter = "*";    // 监控的新生成的文件格式
                    watch.IncludeSubdirectories = true; // 监控子目录
                    watch.Created += new FileSystemEventHandler(OnChangFile);   // 有文件创建则触发事件(watch.Created)
                    watch.EnableRaisingEvents = true; // 启动监控
           }
                  private void OnChangFile(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("有新文件产生!");
            Console.WriteLine(e.FullPath);  // 打印文件的路径
            Console.WriteLine(e.Name);  // 打印文件的名称
        }       

二、多路条径监控

   main(){
   			    string path1 = "D:\Program Files (x86)\Microsoft Office";   // 监控路径1
   			    string path2 = "D:\Program Files (x86)\Microsoft Visual Studio 14.0\lib";  // 监控路径2
                string[] pathArray = { path1, path2};
                for (int i = 0; i < pathArray.Count(); i++)
                {
                    FileSystemWatcher watch = new FileSystemWatcher();
                    watch.Path = pathArray[i];
                    watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName |                          
                    NotifyFilters.DirectoryName;
                    watch.Filter = "*"; 
                    watch.IncludeSubdirectories = true; // 监控子目录
                    watch.Created += new FileSystemEventHandler(OnChangFile);
                    watch.EnableRaisingEvents = true;
                }
                  private void OnChangFile(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("有新文件产生!");
            Console.WriteLine(e.FullPath);  // 打印文件的路径
            Console.WriteLine(e.Name);  // 打印文件的名称
        }
        }

博客主要还是为了巩固自己,如对他人有帮助,实在是我的荣幸!

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FileSystemWatcher是一个.NET框架提供的用于监控文件系统变更的组件。通过FileSystemWatcher,我们可以在文件发生变化时进行相应的处理,例如拷贝、删除、移动等等。使用FileSystemWatcher,我们可以在Windows中监控文件夹、文件以及网络共享的文件系统变更。 FileSystemWatcher为我们提供了多种事件来观察文件系统变更的细节。当文件夹、文件或网络共享上的文件发生变更时,FileSystemWatcher会触发相应的事件。事件列表包括Changed、Created、Deleted、Renamed。对于每个事件,我们可以通过FileSystemEventArgs对象来获得发生变更的文件名、路径、类型等信息。 在使用FileSystemWatcher之前,我们需要确定监控的路径和需要处理的事件类型。如果要监控多个路径,可以使用多个FileSystemWatcher实例来进行处理。需要注意的是,FileSystemWatcher可能会发生一些意外的错误,例如监控文件的时候文件正在被修改,或者文件已被删除,此时我们需要采取合适的方法来处理这些错误。我们也可以调整FileSystemWatcher的属性来控制监控的精度和延迟等参数,从而更好地适应应用场景。 总之,使用FileSystemWatcher可以方便地监控文件系统变更,从而更好地实现文件系统管理。例如,在文件复制或移动的过程中,我们可以使用FileSystemWatcher监控是否成功地复制或移动文件。同时,对于进行监控的路径和事件类型,我们可以根据实际情况进行调整和优化,从而更好地适用于不同的应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值