C#监控文件夹变化的方法
作者:shichen2014 时间:2023-03-26 09:39:38
本文实例讲述了C#监控文件夹变化的方法。分享给大家供大家参考。具体实现方法如下:
当需要监控某一文件,FileSystemWatcher类提供了Created, Deleted,Rename等事件。
就拿FileSystemWatcher的Created事件来说,该事件类型是FileSystemEventHandler。
public delgate void FileSystemEventHandler(Object sender, FileSystemEventArgs e)
其中参数:
sender表示事件的发起者
e表示事件参数
在C盘创建temp文件夹。
客户端。
class Program
{
static void Main(string[] args)
{
var watcher = new FileSystemWatcher(@"c:\temp");
watcher.EnableRaisingEvents = true; //启用FileSystemWatcher
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.Deleted += new FileSystemEventHandler(OnDeleted);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
Console.ReadKey();
}
static void OnDeleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine("{0}:{1}", e.ChangeType, e.Name);
}
static void OnCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine("{0}:{1}", e.ChangeType, e.Name);
}
static void OnRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine("{0}:原文件名{1},新文件名{2}", e.ChangeType, e.OldName, e.Name);
}
}
当然,可以通过 watcher.Renamed += OnCreated;注册事件。
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,监控,文件夹
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis 中的sql批量修改方法实现
2022-12-10 23:36:51
C#基于TimeSpan实现倒计时效果的方法
2021-10-19 00:06:04
![](https://img.aspxhome.com/file/2023/6/98296_0s.png)
Android自定义样式圆角dialog对话框
2023-11-04 20:34:10
![](https://img.aspxhome.com/file/2023/5/99785_0s.jpg)
Android WebView 的简单使用
2021-09-01 11:52:54
![](https://img.aspxhome.com/file/2023/1/116011_0s.png)
浅谈SpringCache与redis集成实现缓存解决方案
2022-10-12 01:11:17
![](https://img.aspxhome.com/file/2023/8/95958_0s.png)
Java接口和抽象类有什么区别
2021-08-21 07:01:18
Unity3D绘制地形的实现方法
2022-12-01 01:06:46
![](https://img.aspxhome.com/file/2023/1/122761_0s.gif)
C/C++ Qt StatusBar底部状态栏应用教程
2023-09-29 16:25:45
![](https://img.aspxhome.com/file/2023/0/117790_0s.png)
Android实现微信登录的示例代码
2022-04-04 15:55:56
![](https://img.aspxhome.com/file/2023/8/102618_0s.png)
图解二叉树的三种遍历方式及java实现代码
2022-02-08 12:03:32
![](https://img.aspxhome.com/file/2023/7/83027_0s.png)
Android开源AndroidSideMenu实现抽屉和侧滑菜单
2023-10-09 09:24:51
![](https://img.aspxhome.com/file/2023/2/111722_0s.jpg)
Java中的interrupted()和isInterrupted()
2023-06-17 22:16:31
![](https://img.aspxhome.com/file/2023/6/57596_0s.png)
mybatis原理概述入门教程
2023-10-08 13:10:57
Android实现Banner界面广告图片循环轮播(包括实现手动滑动循环)
2022-02-06 17:05:35
![](https://img.aspxhome.com/file/2023/1/93261_0s.gif)
Android自定义View实现拖动自动吸边效果
2021-11-18 03:00:38
![](https://img.aspxhome.com/file/2023/3/93273_0s.gif)
Java算法设计与分析分治算法
2022-04-02 08:07:15
![](https://img.aspxhome.com/file/2023/9/92549_0s.png)
java实现udp通讯的代码
2022-07-18 13:28:52
c# 防火墙添加/删除 特定端口的示例
2023-12-22 21:56:41
![](https://img.aspxhome.com/file/2023/7/98247_0s.png)
C#打印日志的方法总结
2022-02-08 22:32:00
![](https://img.aspxhome.com/file/2023/4/96334_0s.jpg)
C#实现的Win32控制台线程计时器功能示例
2022-12-19 20:29:20