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);
}
}
}
}
广积粮,筑高墙,缓称王