//全局变量
public static FileSystemWatcher Watcher;
/// <summary>
/// 设置监听配置
/// </summary>
/// <returns>ture/false</returns>
public static Boolean SetMonitorFile()
{
try
{
Watcher = new FileSystemWatcher();
Watcher.Filter = "*.txt";
Watcher.Path = "d:\"; //路径
Watcher.NotifyFilter = NotifyFilters.Size; //文件大小改变
//文件改变事件
Watcher.Changed += new FileSystemEventHandler(Watcher_Changed); ;
//文件创建事件
Watcher.Created += new FileSystemEventHandler(Watcher_Created);
//文件删除事件
Watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);
//文件重命名事件
Watcher.Renamed += new RenamedEventHandler(Watcher_Renamed);
//设置监听子目录
Watcher.IncludeSubdirectories = false;
//开始进行监听(其实此处是标示是否进行事件监听和抛出)
Watcher.EnableRaisingEvents = true;
//watcher.EndInit();
return true;
}
catch (Exception ex)
{
return false;
}
}