经常我们会碰到如果某个文件被更改,能够做出对应的操作。
public class FileWatch
{
public static FileSystemWatcher _fileWatcher;
private static readonly object _lockObj = new object();
private static void SetWatch(string path)
{
if(_fileWatcher==null)
{
lock(_lockObj)
{
if(_fileWatcher==null)
{
FileInfo file=new FileInfo(path);
_fileWatcher=new FileSystemWatcher(file.DirectoryName);
_fileWatcher.Filter=file.Name;
_fileWatcher.NotifyFilter=NotifyFilters.LastWrite;
_fileWatcher.EnableRaisingEvents=true;
_fileWatcher.Changed+=new FileSystemEventHandler(_watcher_Changed);
}
}
}
}
static void _watcher_Changed(object sender,FileSystemEventArgs e)
{
try
{
HttpRuntime.UnloadAppDomain();
}
catch
{
}
}
}