FileSystemWatcher 用法

FileSystemWatcher 用法1.FileSystemWatcher基础在应用FileSystemWatcher对象之前,必须了解这个对象的一些基本属性和事件。毫无疑问,这个对象的最重要的属性为“EnableRaisingEvents”属性。这个属性决定对象在收到改变通知时是否提交事件。如果EnableRaisingEvents属性设为假,对象将不会提交改变事件。如果设为真,它将提交改变事件。下面是在应用FileSys

大家好,又见面了,我是你们的朋友全栈君。

1.FileSystemWatcher基础

在应用FileSystemWatcher对象之前,必须了解这个对象的一些基本属性和事件。毫无疑问,这个对象的最重要的属性为“EnableRaisingEvents”属性。

这个属性决定对象在收到改变通知时是否提交事件。如果EnableRaisingEvents属性设为假,对象将不会提交改变事件。如果设为真,它将提交改变事件。下面是在应用FileSystemWatcher对象时将要用到的其它一些重要属性/事件:

属性:

Path——这个属性告诉FileSystemWatcher它需要监控哪条路径。例如,如果我们将这个属性设为“C:Temp”,对象就监控那个目录发生的所有改变。
IncludeSubDirectories——这个属性说明FileSystemWatcher对象是否应该监控子目录中发生的改变。
Filter——这个属性允许你过滤掉某些类型的文件发生的变化。例如,如果我们只希望在TXT文件被修改/新建/删除时提交通知,可以将这个属性设为“*txt”。在处理高流量或大型目录时,使用这个属性非常方便。

事件

Changed——当被监控的目录中有一个文件被修改时,就提交这个事件。值得注意的是,这个事件可能会被提交多次,即使文件的内容仅仅发生一项改变。这是由于在保存文件时,文件的其它属性也发生了改变。
Created——当被监控的目录新建一个文件时,就提交这个事件。如果你计划用这个事件移动新建的事件,你必须在事件处理器中写入一些错误处理代码,它能处理当前文件被其它进程使用的情况。之所以要这样做,是因为Created事件可能在建立文件的进程释放文件之前就被提交。如果你没有准备正确处理这种情况的代码,就可能出现异常。
Deleted——当被监控的目录中有一个文件被删除,就提交这个事件。
Renamed——当被监控的目录中有一个文件被重命名,就提交这个事件。
注:如果你没有将EnableRaisingEvents设为真,系统不会提交任何一个事件。如果有时FileSystemWatcher对象似乎无法工作,请首先检查EnableRaisingEvents,确保它被设为真。

事件处理

当FileSystemWatcher调用一个事件处理器时,它包含两个自变量——一个叫做“sender”的对象和一个叫做“e”的FileSystemEventArgs对象。我们感兴趣的自变量为FileSystemEventArgs自变量。这个对象中包含有提交事件的原因。以下是FileSystemEventArgs对象的一些属性:

Name——这个属性中使事件被提交的文件的名称。其中并不包含文件的路径——只包含使用事件被提交的文件或目录名称。
ChangeType——这是一个WatcherChangeTypes,它指出要提交哪个类型的事件。其有效值包括:
○Changed
○Created
○Deleted
○Renamed
FullPath——这个属性中包含使事件被提交的文件的完整路径,包括文件名和目录名。

2.對多文件夾的監視實例

FileSystemWatcher 用法
public
 
static
 
void
 Run(ArrayList  ss) 
FileSystemWatcher 用法FileSystemWatcher 用法        



FileSystemWatcher 用法            
foreach (string s in ss) 
FileSystemWatcher 用法FileSystemWatcher 用法            
{               
FileSystemWatcher 用法                    FileSystemWatcher watcher 
= new FileSystemWatcher(); 
FileSystemWatcher 用法                    watcher.Path 
= s;//@”d:DownLoads”;//args[1]; 
FileSystemWatcher 用法FileSystemWatcher 用法
                    /* Watch for changes in LastAccess and LastWrite times, and  
FileSystemWatcher 用法                       the renaming of files or directories. 
*/
 
FileSystemWatcher 用法                    watcher.NotifyFilter 
= NotifyFilters.LastAccess | NotifyFilters.LastWrite 
FileSystemWatcher 用法                    
| NotifyFilters.FileName | NotifyFilters.DirectoryName; 
FileSystemWatcher 用法                    
// Only watch text files. 
FileSystemWatcher 用法
                    watcher.Filter = *.flv
FileSystemWatcher 用法
FileSystemWatcher 用法                    
// Add event handlers. 
FileSystemWatcher 用法
                    watcher.Changed += new FileSystemEventHandler(OnChanged); 
FileSystemWatcher 用法                    watcher.Created 
+= new FileSystemEventHandler(OnCreated); 
FileSystemWatcher 用法                    watcher.Deleted 
+= new FileSystemEventHandler(OnChanged); 
FileSystemWatcher 用法                    watcher.Renamed 
+= new RenamedEventHandler(OnChanged); 
FileSystemWatcher 用法
FileSystemWatcher 用法                    
// Begin watching. 
FileSystemWatcher 用法
                    watcher.EnableRaisingEvents = true
FileSystemWatcher 用法             
FileSystemWatcher 用法            }
       
FileSystemWatcher 用法        }

 
FileSystemWatcher 用法        

public
 
void
 OnChanged(
object
 source, FileSystemEventArgs e)
FileSystemWatcher 用法FileSystemWatcher 用法        


{

FileSystemWatcher 用法            
//文件改變後的代碼
FileSystemWatcher 用法
        }


FileSystemWatcher 用法
FileSystemWatcher 用法        

public
 
void
 OnCreated(
object
 source, FileSystemEventArgs e)
FileSystemWatcher 用法FileSystemWatcher 用法        


{

FileSystemWatcher 用法            
//添加文件後的代碼
FileSystemWatcher 用法
        }


FileSystemWatcher 用法
FileSystemWatcher 用法        

public
 
void
 OnDeleted(
object
 source, FileSystemEventArgs e)
FileSystemWatcher 用法FileSystemWatcher 用法        


{

FileSystemWatcher 用法            
//文件刪除後的代碼
FileSystemWatcher 用法
        }


FileSystemWatcher 用法
FileSystemWatcher 用法        

public
 
void
 OnRenamed(
object
 source, RenamedEventArgs e)
FileSystemWatcher 用法FileSystemWatcher 用法        


{

FileSystemWatcher 用法            
//文件重命名後的代碼
FileSystemWatcher 用法
         }

 
FileSystemWatcher 用法
FileSystemWatcher 用法

使用System.IO.FileSystemWatcher时,通常会想在检测到文件创建之后,扫描文件的内容,对之进行一定的处理。但是当我们的程序接到通知时,创建文件的进程可能还在写数据,这时如果想要打开这个文件会抛出异常。

似乎没有什么好办法来解决这个问题,除了最笨的一种:

 

FileSystemWatcher 用法
FileSystemWatcher watcher 
=
 
new
 FileSystemWatcher(directory, 

*.txt

);
FileSystemWatcher 用法       watcher.NotifyFilter 

=
 NotifyFilters.FileName;
FileSystemWatcher 用法       watcher.Created 

+=
 FileCreated;
FileSystemWatcher 用法       watcher.EnableRaisingEvents 

=
 
true
;
FileSystemWatcher 用法
FileSystemWatcher 用法        

private
 
void
 FileCreated(
object
 sender, FileSystemEventArgs e)
FileSystemWatcher 用法FileSystemWatcher 用法        


{

FileSystemWatcher 用法            
while (!IsFileReady(e.FullPath))
FileSystemWatcher 用法FileSystemWatcher 用法            
{

FileSystemWatcher 用法                
if (!File.Exists(e.FullPath))
FileSystemWatcher 用法                    
return;
FileSystemWatcher 用法                Thread.Sleep(
100);
FileSystemWatcher 用法            }

FileSystemWatcher 用法            
//在这里进行文件处理。。。
FileSystemWatcher 用法
        }


FileSystemWatcher 用法
FileSystemWatcher 用法        

bool
 IsFileReady(
string
 filename)
FileSystemWatcher 用法FileSystemWatcher 用法        


{

FileSystemWatcher 用法            FileInfo fi 
= new FileInfo(filename);
FileSystemWatcher 用法            FileStream fs
=null;
FileSystemWatcher 用法            
try
FileSystemWatcher 用法FileSystemWatcher 用法            
{

FileSystemWatcher 用法                 fs 
= fi.Open(FileMode.Open, FileAccess.ReadWrite,
FileSystemWatcher 用法            FileShare.None);
FileSystemWatcher 用法                 
return true;
FileSystemWatcher 用法            }

FileSystemWatcher 用法
FileSystemWatcher 用法            
catch(IOException)
FileSystemWatcher 用法FileSystemWatcher 用法            
{

FileSystemWatcher 用法                
return false;
FileSystemWatcher 用法            }

FileSystemWatcher 用法
FileSystemWatcher 用法            
finally
FileSystemWatcher 用法FileSystemWatcher 用法            
{

FileSystemWatcher 用法                        
if(fs!=null)
FileSystemWatcher 用法                            fs.Close();
FileSystemWatcher 用法            }

FileSystemWatcher 用法        }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149928.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js垃圾处理机制_java中垃圾回收有什么目的

    js垃圾处理机制_java中垃圾回收有什么目的文章目录前置知识堆栈栈堆执行上下文与作用域链执行上下文作用域链一、JavaScript中怎么被定义为垃圾使用局部变量使用对象概括二、两种回收策略标记清理引用计数概括三、什么时候执行垃圾回收关于ChromeV8引擎的GC分代回收指针与活跃对象的区分回收的执行周期四、内存问题五、Es6WeakMap参考文章前置知识堆栈栈什么是栈栈其实是一种数据结构,有着先进后出,后进先出的特性,用生活中的事物来理解最形象的就是汉诺塔了。我们在栈中存储的数据就像汉诺塔的盘子一样,最先放进去在最下面,最后放入的盘.

    2022年10月9日
    0
  • lock的用法_try block

    lock的用法_try blocktryLock有两个重载的方法,分别如下:booleantryLock();booleantryLock(longtime,TimeUnitunit)throwsInterruptedException;tryLock()会立马返回一个布尔值,如果获得了锁就返回false;如果没有获得锁就返回true。无论是返回true还是false,都会继续执行之后的代码。tryLock(longtime,TimeUnitunit)会等待指定的时间,如果时间到了还没获得锁就返

    2022年10月15日
    0
  • ActiveMQ 从零开始 学习日志(一)

    ActiveMQ 从零开始 学习日志(一)ActiveMQ 从零开始 学习日志(一)

    2022年4月20日
    38
  • echarts 图表_ECHARTS

    echarts 图表_ECHARTS旭日图(Sunburst)由多层的环形图组成,在数据结构上,内圈是外圈的父节点。因此,它既能像饼图一样表现局部和整体的占比,又能像矩形树图一样表现层级关系。ECharts创建旭日图很简单,只需要在series配置项中声明类型为sunburst即可,data数据结构以树形结构声明,看下一个简单的实例:varoption={series:{type:’sunburst’,data:…

    2022年9月26日
    0
  • 函数指针

    函数指针前言:先看两个基础,函数指针和extern关键字,然后由一个具体的例子,具体使用下函数指针。一、基础函数指针:即指向函数的指针,本质还是一个指针。函数指针的声明:返回值类型(*指针变量名)

    2022年7月4日
    18
  • Java IO流知识点总结

    Java IO流知识点总结本主要对java的IO流知识点做比较全面的总结,但是没有很深入阐述。

    2022年5月20日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号