谈谈C#文件监控对象FileSystemWatcher使用感受

2023-10-31 23:32

本文主要是介绍谈谈C#文件监控对象FileSystemWatcher使用感受,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近在项目中有这么个需求,就是得去实时获取某个在无规律改变的文本文件中的内容。首先想到的是用程序定期去访问这个文件,因为对实时性要求很高,间隔不能超过1S,而且每次获取到文本内容都要去分发给WEB服务器做别的操作,而那个文本的写入有时候会频繁,1秒可能多次,但是也有可能在相当长一段时间内是没有任何写入的。

这样一来如果每秒都去访问文件的话,一个是IO问题,还有就是每次操作都会引起后端一系列程序的反应,文本在长时间内无写入的话,一秒一次的触发一系列徒劳的事情太不可取了。

最终发现了c#中的FileSystemWatcher对象,在应用FileSystemWatcher之前,首先了解一下这个对象的基本属性和事件,首先普及一下FileSystemWatcher基本知识。

FileSystemWatcher基础

属性:

    Path——这个属性告诉FileSystemWatcher它需要监控哪条路径。例如,如果我们将这个属性设为“C:\test”,对象就监控test目录下所有文件发生的所有改变(包括删除,修改,创建,重命名)。

    IncludeSubDirectories——这个属性说明FileSystemWatcher对象是否应该监控子目录中(所有文件)发生的改变。

    Filter——这个属性允许你过滤掉某些类型的文件发生的变化。例如,如果我们只希望在TXT文件被修改/新建/删除时提交通知,可以将这个属性设为“*txt”。在处理高流量或大型目录时,使用这个属性非常方便。

NotifyFilter——获取或设置要监视的更改类型。可以进一步的过滤要监控的更改类型,如watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite

           | NotifyFilters.FileName | NotifyFilters.DirectoryName;

事件:

    Changed——当被监控的目录中有一个文件被修改时,就提交这个事件。值得注意的是,这个事件可能会被提交多次,即使文件的内容仅仅发生一项改变。这是由于在保存文件时,文件的其它属性也发生了改变。

    Created——当被监控的目录新建一个文件时,就提交这个事件。如果你计划用这个事件移动新建的事件,你必须在事件处理器中写入一些错误处理代码,它能处理当前文件被其它进程使用的情况。之所以要这样做,是因为Created事件可能在建立文件的进程释放文件之前就被提交。如果你没有准备正确处理这种情况的代码,就可能出现异常。

    Deleted——当被监控的目录中有一个文件被删除,就提交这个事件。

    Renamed——当被监控的目录中有一个文件被重命名,就提交这个事件。 

 

注:如果你没有将EnableRaisingEvents设为真,系统不会提交任何一个事件。如果有时FileSystemWatcher对象似乎无法工作,请首先检查EnableRaisingEvents,确保它被设为真。

 

事件处理

 

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

 

属性:

 

  Name——这个属性中使事件被提交的文件的名称。其中并不包含文件的路径——只包含使用事件被提交的文件或目录名称。

  ChangeType——这是一个WatcherChangeTypes,它指出要提交哪个类型的事件。其有效值包括:

  Changed

  Created

  Deleted

  Renamed

  FullPath——这个属性中包含使事件被提交的文件的完整路径,包括文件名和目录名。

 

注意:FileSystemEventArgs对象是监控文件夹下有文件创建、删除、修改时的自变量,如果是重命名的话为RenamedEventArgs对象此时除了FileSystemEventArgs对象的属性值,多了一个OldFullPath,为重命名之前的文件名。

 

以上为FileSystemEventArgs的基本知识,大部分是从网上搜找的然后自己稍微整理了一下。

 

下面为简单用法:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.IO;
namespace test
{
     class Program
     {
         static void Main( string [] args)
         {
             WatcherStrat( @"C:\test" , "*.txt" );
             //由于是控制台程序,加个输入避免主线程执行完毕,看不到监控效果
             Console.ReadKey();
         }
       
         private static void WatcherStrat( string path, string filter)
         {
             FileSystemWatcher watcher = new FileSystemWatcher();
             watcher.Path = path;
             watcher.Filter = filter;
             watcher.Changed += new FileSystemEventHandler(OnProcess);
             watcher.Created += new FileSystemEventHandler(OnProcess);
             watcher.Deleted += new FileSystemEventHandler(OnProcess);
             watcher.Renamed += new RenamedEventHandler(OnRenamed);
             watcher.EnableRaisingEvents = true ;
         }
         private static void OnProcess( object source, FileSystemEventArgs e)
         {
             if (e.ChangeType == WatcherChangeTypes.Created)
             {
                 OnCreated(source, e);
             }
             else if (e.ChangeType == WatcherChangeTypes.Changed)
             {
                 OnChanged(source, e);
             }
             else if (e.ChangeType == WatcherChangeTypes.Deleted)
             {
                 OnDeleted(source, e);
             }
         }
         private static void OnCreated( object source, FileSystemEventArgs e)
         {
             Console.WriteLine( "文件新建事件处理逻辑" );
             
         }
         private static void OnChanged( object source, FileSystemEventArgs e)
         {
             Console.WriteLine( "文件改变事件处理逻辑" );
         }
         private static void OnDeleted( object source, FileSystemEventArgs e)
         {
             Console.WriteLine( "文件删除事件处理逻辑" );
         }
         private static void OnRenamed( object source, RenamedEventArgs e)
         {
             Console.WriteLine( "文件重命名事件处理逻辑" );
         }
     }
}

 

 

 

 

 

 

用上面的方法会发现,在一次文本文件变化的时候OnChanged事件会触发两次,这是因为除了文本内容变化之外还有文件其他的属性也变化了例如修改时间。

     

     为了解决这问题,也便于项目当中实际使用,写了下面几个类来实际使用:

 主方法:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.IO;
namespace test
{
     class Program
     {
         static void Main( string [] args)
         {
             MyFileSystemWather myWather = new MyFileSystemWather( @"C:\test" , "*.txt" );
             myWather.OnChanged += new FileSystemEventHandler(OnChanged);
             myWather.OnCreated += new FileSystemEventHandler(OnCreated);
             myWather.OnRenamed += new RenamedEventHandler(OnRenamed);
             myWather.OnDeleted += new FileSystemEventHandler(OnDeleted);
             myWather.Start();
             //由于是控制台程序,加个输入避免主线程执行完毕,看不到监控效果
             Console.ReadKey();
         }
         private static void OnCreated( object source, FileSystemEventArgs e)
         {
             Console.WriteLine( "文件新建事件处理逻辑" );
             
         }
         private static void OnChanged( object source, FileSystemEventArgs e)
         {
             Console.WriteLine( "文件改变事件处理逻辑" );
         }
         private static void OnDeleted( object source, FileSystemEventArgs e)
         {
             Console.WriteLine( "文件删除事件处理逻辑" );
         }
         private static void OnRenamed( object source, RenamedEventArgs e)
         {
             Console.WriteLine( "文件重命名事件处理逻辑" );
         }
     }
}

 

 

  WatcherProcess类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.IO;
namespace test
{
     public class WatcherProcess
     {
         private object sender;
         private object eParam;
         public event RenamedEventHandler OnRenamed;
         public event FileSystemEventHandler OnChanged;
         public event FileSystemEventHandler OnCreated;
         public event FileSystemEventHandler OnDeleted;
         public event Completed OnCompleted;
         public WatcherProcess( object sender, object eParam)
         {
             this .sender = sender;
             this .eParam = eParam;
         }
         public void Process()
         {
             if (eParam.GetType() == typeof (RenamedEventArgs))
             {
                 OnRenamed(sender, (RenamedEventArgs)eParam);
                 OnCompleted(((RenamedEventArgs)eParam).FullPath);
             }
             else
             {
                 FileSystemEventArgs e = (FileSystemEventArgs)eParam;
                 if (e.ChangeType == WatcherChangeTypes.Created)
                 {
                     OnCreated(sender, e);
                     OnCompleted(e.FullPath);
                 }
                 else if (e.ChangeType == WatcherChangeTypes.Changed)
                 {
                     OnChanged(sender, e);
                     OnCompleted(e.FullPath);
                 }
                 else if (e.ChangeType == WatcherChangeTypes.Deleted)
                 {
                     OnDeleted(sender, e);
                     OnCompleted(e.FullPath);
                 }
                 else
                 {
                     OnCompleted(e.FullPath);
                 }
             }
         }
     }
}

 

  

MyFileSystemWather类:

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections;
using System.IO;
using System.Threading;
namespace test
{
     public delegate void Completed( string key);
     public class MyFileSystemWather
     {
         private FileSystemWatcher fsWather;
         private Hashtable hstbWather;
         public event RenamedEventHandler OnRenamed;
         public event FileSystemEventHandler OnChanged;
         public event FileSystemEventHandler OnCreated;
         public event FileSystemEventHandler OnDeleted;
         /// <summary>
         /// 构造函数
         /// </summary>
         /// <param name="path">要监控的路径</param>
         public MyFileSystemWather( string path, string filter)
         {
             if (!Directory.Exists(path))
             {
                 throw new Exception( "找不到路径:" + path);
             }
             hstbWather = new Hashtable();
             fsWather = new FileSystemWatcher(path);
             // 是否监控子目录
             fsWather.IncludeSubdirectories = false ;
             fsWather.Filter = filter;
             fsWather.Renamed += new RenamedEventHandler(fsWather_Renamed);
             fsWather.Changed += new FileSystemEventHandler(fsWather_Changed);
             fsWather.Created += new FileSystemEventHandler(fsWather_Created);
             fsWather.Deleted += new FileSystemEventHandler(fsWather_Deleted);
         }
         /// <summary>
         /// 开始监控
         /// </summary>
         public void Start()
         {
             fsWather.EnableRaisingEvents = true ;
         }
         /// <summary>
         /// 停止监控
         /// </summary>
         public void Stop()
         {
             fsWather.EnableRaisingEvents = false ;
         }
         /// <summary>
         /// filesystemWatcher 本身的事件通知处理过程
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void fsWather_Renamed( object sender, RenamedEventArgs e)
         {
             lock (hstbWather)
             {
                 hstbWather.Add(e.FullPath, e);
             }
             WatcherProcess watcherProcess = new WatcherProcess(sender, e);
             watcherProcess.OnCompleted += new Completed(WatcherProcess_OnCompleted);
             watcherProcess.OnRenamed += new RenamedEventHandler(WatcherProcess_OnRenamed);
             Thread thread = new Thread(watcherProcess.Process);
             thread.Start();
         }
         private void WatcherProcess_OnRenamed( object sender, RenamedEventArgs e)
         {
             OnRenamed(sender, e);
         }
         private void fsWather_Created( object sender, FileSystemEventArgs e)
         {
             lock (hstbWather)
             {
                 hstbWather.Add(e.FullPath, e);
             }
             WatcherProcess watcherProcess = new WatcherProcess(sender, e);
             watcherProcess.OnCompleted += new Completed(WatcherProcess_OnCompleted);
             watcherProcess.OnCreated += new FileSystemEventHandler(WatcherProcess_OnCreated);
             Thread threadDeal = new Thread(watcherProcess.Process);
             threadDeal.Start();
         }
         private void WatcherProcess_OnCreated( object sender, FileSystemEventArgs e)
         {
             OnCreated(sender, e);
         }
         private void fsWather_Deleted( object sender, FileSystemEventArgs e)
         {
             lock (hstbWather)
             {
                 hstbWather.Add(e.FullPath, e);
             }
             WatcherProcess watcherProcess = new WatcherProcess(sender, e);
             watcherProcess.OnCompleted += new Completed(WatcherProcess_OnCompleted);
             watcherProcess.OnDeleted += new FileSystemEventHandler(WatcherProcess_OnDeleted);
             Thread tdDeal = new Thread(watcherProcess.Process);
             tdDeal.Start();
         }
         private void WatcherProcess_OnDeleted( object sender, FileSystemEventArgs e)
         {
             OnDeleted(sender, e);
         }
         private void fsWather_Changed( object sender, FileSystemEventArgs e)
         {
             if (e.ChangeType == WatcherChangeTypes.Changed)
             {
                 if (hstbWather.ContainsKey(e.FullPath))
                 {
                     WatcherChangeTypes oldType = ((FileSystemEventArgs)hstbWather[e.FullPath]).ChangeType;
                     if (oldType == WatcherChangeTypes.Created || oldType == WatcherChangeTypes.Changed)
                     {
                         return ;
                     }
                 }
             }
             lock (hstbWather)
             {
                 hstbWather.Add(e.FullPath, e);
             }
             WatcherProcess watcherProcess = new WatcherProcess(sender, e);
             watcherProcess.OnCompleted += new Completed(WatcherProcess_OnCompleted);
             watcherProcess.OnChanged += new FileSystemEventHandler(WatcherProcess_OnChanged);
             Thread thread = new Thread(watcherProcess.Process);
             thread.Start();
         }
         private void WatcherProcess_OnChanged( object sender, FileSystemEventArgs e)
         {
             OnChanged(sender, e);
         }
         public void WatcherProcess_OnCompleted( string key)
         {
             lock (hstbWather)
             {
                 hstbWather.Remove(key);
             }
         }
     }
}

 使用了线程安全的Hashtable来处理一次改变触发两次事件的问题,要注意的是在实际项目使用中,在通过监控文件事情触发时开一个线程WatcherProcess去处理自己业务逻辑的时候,不管业务逻辑成功或者失败(例如有异常抛出一定要try一下)一定要让WatcherProcess Completed也就是MyFileSystemWatherWatcherProcess_OnCompleted执行去移除对应变化文件的Hashtablekey,不然下次此文件改变时是无法触发你的业务逻辑的。

    

     还有就是在进行文件监控的时候, 被监控文件在写入的时候,是会有I/O冲突的,即使写入文件是FileShare.Read的也会出现,要真正解决貌似只有FileMaping方法,但是我的项目中文本的写入软件不是我们能控制的,所以只有用处理异常的方法来解决。


轉自:http://www.cnblogs.com/zhaojingjing/archive/2011/01/21/1941586.html

这篇关于谈谈C#文件监控对象FileSystemWatcher使用感受的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/318887

相关文章

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代