作者: suver
查看: 2798|回复: 4
打印 上一主题 下一主题

[源码] 【文件监控】再给大家提供一个文件监控的例子

[复制链接]
跳转到指定楼层
楼主
suver 发表于 2014-10-21 09:13:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查看: 2798|回复: 4
1、支持对目标目录下的文件进行监控,监控动作又修改、删除、创建
2、支持对目录下文件的过滤,比如只针对某一个文件进行监控
3、修改了原来例子中输出监控日志,增加了一个执行启动应用的功能,比如监控到某一个文件变动,可以执行另外一个程序
   可以用来实现对某类写临时文件的程序,进行中间数据截取
关键代码和上次一个贴友发的一样,只是这个是winfom的

[C#] 纯文本查看 复制代码
private void startActivityMonitoring(string sPath)
        {
            if (sPath.Length < 3)
            {
                MessageBox.Show("You have to enter a folder to monitor.",
                    "Hey..!", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                abortAcitivityMonitoring();
            }
            else
            {
                // This is the path we want to monitor
                _watchFolder.Path = sPath;

                // Make sure you use the OR on each Filter because we need to monitor
                // all of those activities

                _watchFolder.NotifyFilter = System.IO.NotifyFilters.DirectoryName;

                _watchFolder.NotifyFilter = _watchFolder.NotifyFilter | System.IO.NotifyFilters.FileName;
                _watchFolder.NotifyFilter = _watchFolder.NotifyFilter | System.IO.NotifyFilters.Attributes;

                // Now hook the triggers(events) to our handler (eventRaised)
                _watchFolder.Changed += new FileSystemEventHandler(eventRaised);
                _watchFolder.Created += new FileSystemEventHandler(eventRaised);
                _watchFolder.Deleted += new FileSystemEventHandler(eventRaised);

                // Occurs when a file or directory is renamed in the specific path
                _watchFolder.Renamed += new System.IO.RenamedEventHandler(eventRenameRaised);

                // And at last.. We connect our EventHandles to the system API (that is all
                // wrapped up in System.IO)
                try
                {
                    _watchFolder.EnableRaisingEvents = true;
                }
                catch (ArgumentException)
                {
                    abortAcitivityMonitoring();
                }
            }
        }



评分

参与人数 1金钱 +1 贡献 +1 收起 理由
乔克斯 + 1 + 1 感谢分享,LZ辛苦了~

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 转播转播
回复 论坛版权

使用道具 举报

沙发
 楼主| suver 发表于 2014-10-21 09:14:23 | 只看该作者
我是用来实现对一款收银系统进行中间数据监控的,因为收银系统部允许导出当天销售列表(只能系统中看),所以研究了一下采用这种方式!
板凳
falconet 发表于 2018-3-28 11:49:22 | 只看该作者
非常不错!很有用的
地板
ch_tao 发表于 2018-8-27 19:07:22 | 只看该作者
谢谢楼主
回复

使用道具 举报

5#
go_go007 发表于 2019-5-6 19:08:42 | 只看该作者
这都可以,简直屌到没朋友!
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-5-5 05:05, Processed in 0.647193 second(s), 34 queries , Gzip On.

快速回复 返回顶部 返回列表