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美女图片切换带视觉差

    使用JS实现,多张图片动态切换查看效果:http://hovertree.com/texiao/js/21/效果图:转自:http://hovertree.com/h/bjaf/iamhxcyk.h

    2021年12月22日
    47
  • 何时使用或何时不使用malloc函数

    何时使用或何时不使用malloc函数在初学数据结构时,我们往往不太清楚在定义一个结构体指针时要不要使用malloc函数。例如以下的代码:LINKLIST*initlinklist(){LINKLIST*H=NULL;LINKLIST*S;intx;printf(“请输入链表元素或以-1结束输入”)while(x!=-1){s=(LINKLIST*

    2022年6月7日
    30
  • 动态规划-背包问题

    动态规划-背包问题背包问题是一种组合优化的NP完全问题。有N个物品和容量为W的背包,每个物品都有自己的体积w和价值v,求拿哪些物品可以使得背包所装下的物品的总价值最大。如果限定每种物品只能选择0个或1个,则问题称为0-1背包问题;如果不限定wu’pi…

    2022年7月26日
    9
  • FreeWebHostingArea_skyscraperpage

    FreeWebHostingArea_skyscraperpagehttp://www.cnblogs.com/skyme/archive/2011/10/26/2223984.html

    2022年9月30日
    0
  • Winform控件开发(1)——Label(史上最全)

    Winform控件开发(1)——Label(史上最全)作用:一般用于显示文本或者作为”按钮使用”,当作为显示文本使用时,通过设置label的Text属性实现,当作为“按钮使用时”,在lable的单击事件下注册事件即可,下面详细介绍label的属性:1、Name属性,该属性代表label类对象的名称,通过该属性可以获取到该label对象,如下图:该label对象名称为label1,当然也可以更改为其他名称2、AllowDrop属性,该属性的值是…

    2022年7月26日
    30
  • idea2021.7永久激活码【2021免费激活】

    (idea2021.7永久激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlML…

    2022年3月21日
    44

发表回复

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

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