临视目录内容是否有改变的解读
2015-04-04 16:17
204 查看
http://s.codeproject.com/Articles/271669/Using-FileSystemWatcher-to-monitor-multiple-direct
In a previous example, I have showed how to use the
We can do that by using the following method. First create a class. We’ll call this class
And we can use it to monitor multiple directories as shown below (for this example, I have used a console application and I am only considering the change event):
http://s.codeproject.com/Articles/271669/Using-FileSystemWatcher-to-monitor-multiple-direct
Using FileSystemWatcher to monitor multiple directories
There are times that we need to monitor multiple directories and if any changes are available, invoke a given method.In a previous example, I have showed how to use the
FileSystemWatcherclass to monitor a directory. But there are times that we need to monitor multiple directories and if any changes are available, invoke a given method.
We can do that by using the following method. First create a class. We’ll call this class
Watcher.
1: public class Watcher 2: { 3: 4: public string Directory { get; set; } 5: public string Filter { get; set; } 6: 7: 8: private Delegate _changeMethod; 9: 10: public Delegate ChangeMethod 11: { 12: get { return _changeMethod; } 13: set { _changeMethod = value; } 14: } 15: 16: FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(); 17: 18: public Watcher(string directory, string filter, Delegate invokeMethod) 19: { 20: this._changeMethod = invokeMethod; 21: this.Directory = directory; 22: this.Filter = Filter; 23: } 24: 25: 26: public void StartWatch() 27: { 28: 29: 30: fileSystemWatcher.Filter = this.Filter; 31: fileSystemWatcher.Path = this.Directory; 32: fileSystemWatcher.EnableRaisingEvents = true; 33: 34: fileSystemWatcher.Changed += new FileSystemEventHandler(fileSystemWatcher_Changed); 35: } 36: 37: void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e) 38: { 39: if (_changeMethod != null) 40: { 41: _changeMethod.DynamicInvoke(sender, e); 42: } 43: } 44: }
And we can use it to monitor multiple directories as shown below (for this example, I have used a console application and I am only considering the change event):
1: class Program 2: { 3: delegate void invokeMethodDelegate(object sender, FileSystemEventArgs e); 4: 5: static void Main(string[] args) 6: { 7: 8: invokeMethodDelegate mymethod = new invokeMethodDelegate(InvokeMethod); //公用的全局函数 9: Watcher w1 = new Watcher(@"C:\Directory1", "*.*", mymethod); //这里先后创建两个监视对象,指向共同的全局函数 10: w1.StartWatch(); 11: 12: Watcher w2 = new Watcher(@"D:\Directory2", "*.*", mymethod); 13: w2.StartWatch(); 14: 15: string zRetVal = Console.ReadLine(); 16: 17: 18: } 19: 20: static void InvokeMethod(object sender, FileSystemEventArgs e)//公用的全局函数 21: { 22: Console.WriteLine("Change in file {0}", e.FullPath); 23: } 24: }
所以,看到C#的事件机制,还是比较让人感觉亲切。
相关文章推荐
- 利用ruby数组检查指定目录下的文件是否有改变
- linux下常用小技巧--查找目录下是否存在特定内容的文件
- linux查看目录下所有文件内容中是否包含某个字符串
- 是否想为你的Windows加上一双眼睛,察看使用者在机器上所做的各种操作(例如建立、删除文件;改变文件或目录名字)呢?
- linux查看目录下所有文件内容中是否包含某个字符串
- 文件输入与复制(判断文件和目录是否存在)、读取网页内容
- 监控WebBrowser的内容是否发生改变(转)
- 监控WebBrowser的内容是否发生改变
- VS文件属性(无,编译,内容,嵌入的资源--改变发布网站时,某些文件目录不见了)
- From表单判断内容是否有改变(二)
- 搜索目录下所有文件内容是否包含指定的关键词
- javascript 实现 检查form内容是否发生改变 (非常强大)
- From表单判断内容是否有改变
- gridview如何改变行显示的内容以及设置单元格的是否允许编辑
- Atitit 遍历文件夹目录解决方案与规范 attilax总结 1. 规范 2 1.1. 注意的不要同时改变文件夹内容,增删文件。这样获取到的目录list不会变化 2 1.2. 主义中文名称文件读写
- Linux下的目录及其操作命令(改变工作目录和显示目录内容的命令
- 改变android system目录的权限,使修改该目录下的内容。
- JS验证GridView中文本框内容是否改变并清空
- Linux下的目录及其操作命令(三、改变工作目录和显示目录内容的命令)
- gridview如何改变行显示的内容以及设置单元格的是否允许编辑