惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - sdav

无耻的360是否还值得大家支持? 博客员光写技术博客,咱发篇文学吧.活跃活跃气氛. 解决ORA-12514 TNS:监听程序当前无法识别连接描述符中请求的服务的偷懒办法. MMC创建无法管理单元。 关于ShowModalDialog数据缓存的清除 通用权限架构设计 基于Remoting的应用程序自动更新。 很早前写的用例分析,看看还挺有意思. Excel格式文件上传到数据库 将excel表格导入数据库 将ListView中的内容倒入到Excel - sdav - 博客园 Ext 类视图 Windows Server 2003服务器群集创建和配置指南 PL/SQL Developer 注册码 v7.0.2.1076 万分紧急----求一个ODP.Net(Oracle Data Provider for .NET Release 9.2.0.2.102) 选择奋斗---激励自己 老文了,还是放在这里吧,要不可能丢失了。也不知道谁是作者。 - sdav - 博客园 秀一下咱的自定义表单 工作流 -- 流程节点动作说明
.NET中的文件监视类
sdav · 2008-11-03 · via 博客园 - sdav

<转>

<发布地址>192.168.0.198</发布地址>

最近做的一个项目,涉及到对某个目录的操作的监视,其实在.NET中,使用.NET中的文件监视类,就可以轻松实现对某个目录,以及其子目录的文件监视。比如新建文件、修改文件、删除文件等操作。并且能够以事件的形式通知程序。只要我们在事件委托里面,绑定相应的处理函数即可。.NET的功能果然很强大,只需要几句简单的调用,我们就可以实现这样的监视功能了。

我们以一个例子来实现吧:

    static void Main(string[] args)
        {
            FileSystemWatcher m_Watcher=new FileSystemWatcher();
            m_Watcher.Path=@"d:\test";
            m_Watcher.IncludeSubdirectories=true;
            m_Watcher.Filter="*.*";
            m_Watcher.NotifyFilter=NotifyFilters.LastWrite | NotifyFilters.FileName;
            m_Watcher.Created+=new FileSystemEventHandler(OnChanged);
            m_Watcher.Changed+=new FileSystemEventHandler(OnChanged);
            m_Watcher.Deleted+=new FileSystemEventHandler(OnChanged);
            m_Watcher.EnableRaisingEvents=true;

            Console.WriteLine("Press \'q\' to quit the sample.");
            while(Console.Read()!='q');
        }

        private static void OnChanged(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine(DateTime.Now.ToString()+" {0} {1}",e.ChangeType,e.FullPath);
        }

程序比较简单,先是生成FileSystemWatcher的实例,然后进行相应的设置。我在这里设置要监控的目录
为D:\Test,监控所有文件,也即*.*。然后通过事件,添加响应事件的函数OnChanged到Created、Changed以及Deleted事件中。这样的话,当文件被创建、修改、或者被删除的时候,就能调用我们的OnChanged函数。此函数中的FileSystemEventArgs包含了具体事件的一些信息。比如被修改的文件名、被出发的事件类型等等。通过在回调函数的判断,我们就可以实现响应的操作了。