商讯信箱
用户名: @
密  码:   注册|忘记密码
登录
个人用户经销商
您的位置:首页 > 技术频道 > 正文

c# FileSystemWatcher控件的使用方法

作者:ITPUB论坛  2008-05-09
  示例代碼:
using System.IO; namespace FileSystemWatcherDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); UsingFileSystemWatcher(); } /// <summary> /// 使用FileSystemWatcher方法 /// </summary> void UsingFileSystemWatcher() { //6.2 //FileSystemWatcher:侦听文件系统更改通知,并在目录或目录中的文件发生更改时引发事件。 //获取或设置要监视的目录的路径。 fswWatcher.Path = @"D:\upload"; //获取或设置要监视的更改类型。 fswWatcher.NotifyFilter = NotifyFilters.LastWrite|NotifyFilters.FileName|NotifyFilters.LastAccess ; //获取或设置筛选字符串,用于确定在目录中监视哪些文件。 //此處只能監控某一種文件,不能監控件多種文件,但可以監控所有文件 fswWatcher.Filter = "*.doc"; //获取或设置一个值,该值指示是否监视指定路径中的子目录。 fswWatcher.IncludeSubdirectories = true; #region 6.3 觸發的事件 //文件或目錄創建時事件 fswWatcher.Created += new FileSystemEventHandler(fswWatcher_Created); //文件或目錄變更時事件 fswWatcher.Changed += new FileSystemEventHandler(fswWatcher_Changed); //文件或目錄重命名時事件 fswWatcher.Renamed += new RenamedEventHandler(fswWatcher_Renamed); //文件或目錄刪除時事件 fswWatcher.Deleted += new FileSystemEventHandler(fswWatcher_Deleted); #endregion //获取或设置一个值,该值指示是否启用此组件。 fswWatcher.EnableRaisingEvents = true; } #region 6.4 觸發事件的方法 /// <summary> /// 文件或目錄創建時事件方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void fswWatcher_Created(object sender, FileSystemEventArgs e) { MessageBox.Show("有新文件"); } /// <summary> /// 文件或目錄變更時事件的方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void fswWatcher_Changed(object sender, FileSystemEventArgs e) { } /// <summary> /// 文件或目錄重命名時事件的方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void fswWatcher_Renamed(object sender, RenamedEventArgs e) { } /// <summary> /// 文件或目錄刪除時事件的方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void fswWatcher_Deleted(object sender, FileSystemEventArgs e) { } #endregion } }
1 2
【内容导航】
第1页: 概述 第2页: 第2页
©版权所有。未经许可,不得转载。
[责任编辑:nancy]
[an error occurred while processing this directive]