ASP.NET----监视文件系统

 
  
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace NET.MST.Fifth.UseFileSystem_fsw
{
class UseFS_fsw
{
// 退出程序命令
private const String Exit = " exit " ;
// 监视的文件夹
private const String Folder = " C:\\Test " ;
private FileSystemWatcher _fsw;

public UseFS_fsw()
{
_fsw
= new FileSystemWatcher();
// 建立检测文件夹
if (Directory.Exists(Folder))
Directory.Delete(Folder,
true );
Directory.CreateDirectory(Folder);
_fsw.Path
= Folder;

// 这里选择最后访问文件时间、最后写文件时间、
// 文件名、文件夹名变化时,触发事件
_fsw.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
// 这里选择检测所有的zip文件
_fsw.Filter = " *.zip " ;
// 为这些事件添加处理方法。.
_fsw.Changed += new FileSystemEventHandler(OnChanged);
_fsw.Created
+= new FileSystemEventHandler(OnChanged);
_fsw.Deleted
+= new FileSystemEventHandler(OnChanged);
_fsw.Renamed
+= new RenamedEventHandler(OnRenamed);
// 这里开始所有事件会被触发
_fsw.EnableRaisingEvents = true ;
}

// 处理变化时间,变化可以包含创建、修改和删除
private void OnChanged( object source, FileSystemEventArgs e)
{
Console.WriteLine(
" 文件: " +
e.FullPath
+ " " + e.ChangeType);
}
// 处理重命名事件
private static void OnRenamed( object source, RenamedEventArgs e)
{
Console.WriteLine(
" 文件: {0} 重命名为: {1} " ,
e.OldFullPath, e.FullPath);
}

static void Main( string [] args)
{
try
{
UseFS_fsw ws
= new UseFS_fsw();
while (Console.ReadLine() != Exit) ;
}
finally
{
// 清理测试数据
if (Directory.Exists(Folder))
Directory.Delete(Folder,
true );
}
}
}
}

转载于:https://www.cnblogs.com/brainmao/archive/2011/06/03/2072297.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值