资源简介
这是我自己写的一个程序,用于公司的自动监控业务。本来觉得这个很简单,后来弄了小两天才弄好。在开发过程中,知道要用FileSystemWatcher类,用这个类比较简单,有固定套路。但是技术难点在于,基于事件的处理,onchanged事件的多次触发,还有oncreated事件的处理,因为在刚一创建的时候,该事件即被触发,导致文件还没有创建完成就触发事件,导致程序报异常。多次触发的解决方案是参考网上的大侠说的,在事件处理函数里,先停止监听事件,等处理完成,再开启监听。欢迎大家给出好的建议。网上有很多解决方案,感觉都够让人头大,微软的这个东西,方便是方便,但是用好它却不易,所以微软做的东西也没有按照我党的教育方针,以人为本啊。

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using System.Threading;
namespace monitoringform
{
public partial class Form1 : Form
{
private string source = ConfigurationManager.AppSettings[“source“];
static private string dest = ConfigurationManager.AppSettings[“dest“];
private string filter = ConfigurationManager.AppSettings[“filter“];
FileSystemWatcher watcher = new FileSystemWatcher();
private string ip = ConfigurationManager.AppSettings[“ftpserver“];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender EventArgs e)
{
CreateWatcher();
}
protected void CreateWatcher()
{
watcher.Path = source;// @“\\192.168.6.52\libotest“;
/* Watch for changes in LastAccess and LastWrite times and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = filter;
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(onchanged);
watcher.Created += new FileSystemEventHandler(onchanged);
watcher.Deleted += new FileSystemEventHandler(onchanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
//runlog(“aaa“);
// Begin watching.
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
}
// Define the event handlers.
private static void onchanged(object source FileSystemEventArgs e)
{
// Specify what is done when a file is changed created or deleted.
//Console.WriteLine(“File: “ + e.FullPath + “ “ + e.ChangeType);
if (e.ChangeType == WatcherChangeTypes.Deleted)
{
((FileSystemWatcher)source).EnableRaisingEvents = false;
//string fn = e.Name.Substring(e.Name.LastIndexOf(‘\\‘)+1);
File.Delete(dest + “\\“ + e.Name);
((FileSystemWatcher)source).EnableRaisingEvents = true;
return;
}
//runlog(“File: “ + e.FullPath + “ “ + e.ChangeType);
if (Directory.Exists(e.FullPath))
return;
string destfile;
if (e.ChangeType == WatcherChangeTypes.Created)
{
Thread.Sleep(1000);
//destfile=e.Name.Substring(e.Name.LastIndexOf(‘\\‘)+1);
//File.Copy(e.FullPath dest + “\\“ + e.Name true);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2010-06-12 17:02 monitoringform
目录 0 2010-06-17 12:04 monitoringform\monitoringform
文件 475 2010-06-17 09:57 monitoringform\monitoringform\App.config
目录 0 2010-06-12 17:01 monitoringform\monitoringform\bin
目录 0 2010-06-12 17:11 monitoringform\monitoringform\bin\Debug
目录 0 2010-06-17 11:40 monitoringform\monitoringform\bin\Debug\log
文件 9931 2010-06-12 20:53 monitoringform\monitoringform\bin\Debug\log\2010-06-12.txt
文件 162 2010-06-17 14:24 monitoringform\monitoringform\bin\Debug\log\2010-06-17.txt
文件 20480 2010-06-17 12:04 monitoringform\monitoringform\bin\Debug\monitoringform.exe
文件 475 2010-06-17 09:57 monitoringform\monitoringform\bin\Debug\monitoringform.exe.config
文件 26112 2010-06-17 12:04 monitoringform\monitoringform\bin\Debug\monitoringform.pdb
文件 5632 2005-11-11 22:25 monitoringform\monitoringform\bin\Debug\monitoringform.vshost.exe
文件 475 2010-06-17 09:57 monitoringform\monitoringform\bin\Debug\monitoringform.vshost.exe.config
文件 5135 2010-06-17 12:04 monitoringform\monitoringform\Form1.cs
文件 2029 2010-06-12 17:02 monitoringform\monitoringform\Form1.Designer.cs
文件 5814 2010-06-12 17:02 monitoringform\monitoringform\Form1.resx
文件 3320 2010-06-12 17:08 monitoringform\monitoringform\monitoringform.csproj
目录 0 2010-06-12 17:06 monitoringform\monitoringform\obj
目录 0 2010-06-17 12:04 monitoringform\monitoringform\obj\Debug
文件 842 2010-06-12 17:06 monitoringform\monitoringform\obj\Debug\monitoringform.csproj.GenerateResource.Cache
文件 20480 2010-06-17 12:04 monitoringform\monitoringform\obj\Debug\monitoringform.exe
文件 180 2010-06-12 17:06 monitoringform\monitoringform\obj\Debug\monitoringform.Form1.resources
文件 26112 2010-06-17 12:04 monitoringform\monitoringform\obj\Debug\monitoringform.pdb
文件 180 2010-06-12 17:06 monitoringform\monitoringform\obj\Debug\monitoringform.Properties.Resources.resources
目录 0 2010-06-12 17:02 monitoringform\monitoringform\obj\Debug\TempPE
文件 741 2010-06-17 16:56 monitoringform\monitoringform\obj\monitoringform.csproj.FileListAbsolute.txt
文件 473 2010-06-12 17:02 monitoringform\monitoringform\Program.cs
目录 0 2010-06-12 17:02 monitoringform\monitoringform\Properties
文件 1176 2010-06-12 17:01 monitoringform\monitoringform\Properties\AssemblyInfo.cs
文件 2884 2010-06-12 17:02 monitoringform\monitoringform\Properties\Resources.Designer.cs
............此处省略8个文件信息
- 上一篇:AD9910中文数据手册
- 下一篇:二叉树遍历实验报告
相关资源
- 模拟QQ登陆界面,按钮无效自己写
- 精美千年登陆器(自动更新).rar
- Xpath生成器,自动生成可用的Xpath。
- 51单片机多功能自行车测速仪
- python实现的ftp自动上传、下载脚本
- “暴风一号”(“1KB文件夹快捷方式
- 可自定义导航网站源码
- GDI自绘滚动条
- 自己写的航空订票系统c 版--数据结构
- 武汉大学本科生论文格式自动排版工
- 一个扩展的CComboBox 下拉框 自绘 美化
- 自适应隐写算法wow
- 课程设计蔬菜大棚自动控制系统,包
- 自动复制U盘数据程序
- 自制测量电容的PCB
- 利用语音信号的短时能量与短时过零
- ATM自动取款机系统的分析与设计
- 用FileSystemWatcher控件实现文件同步更新
- 自编用openGL实现3D分形树,分形山
- OpenGL球形贴图自旋程序
- 全自动多功能编码转换工具(URLASCI
- pb (powerbuilder) 编写的自动转台控制
- C (MFC)华容道自动求解
- 在线考试系统VC (MFC)
- 自定义的TabCtrl控件实现
- 自适应滑窗prony非平稳间谐波谐波参数
- c 面试题(面试经验)自己收集自己
- 选择题自动考试系统
- autoit自动化测试小工具及帮助(二)
- BmpButton
评论
共有 条评论