看一下 FileSystemWatcher 类。它监视对文件甚至文件夹的更改。
看这个例子:
使用系统;使用 System.IO;使用 System.Security.Permissions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Run(@"C:\Users\Hanlet\Desktop\Watcher\ConsoleApplication1\bin\Debug");
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run(string path)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path =path;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.xml";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
if(e.FullPath.IndexOf("resource.xml") > - 1)
Console.WriteLine("The file was: " + e.ChangeType);
}
}
}
每当 resource.xml 文件发生某种变化(创建、删除或更新)时,它都会监控并捕获。祝你好运!