//全局變量
public static FileSystemWatcher Watcher;
/// <summary>
/// 設(shè)置監(jiān)聽配置
/// </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); ;
//文件創(chuàng)建事件
Watcher.Created += new FileSystemEventHandler(Watcher_Created);
//文件刪除事件
Watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);
//文件重命名事件
Watcher.Renamed += new RenamedEventHandler(Watcher_Renamed);
//設(shè)置監(jiān)聽子目錄
Watcher.IncludeSubdirectories = false;
//開始進行監(jiān)聽(其實此處是標示是否進行事件監(jiān)聽和拋出)
Watcher.EnableRaisingEvents = true;
//watcher.EndInit();
return true;
}
catch (Exception ex)
{
return false;
}
}
新聞熱點
疑難解答