我正在寻找一种方法来确定特定文件何时更改,通过.NET.(我最终想要的是在文件发生变化后立即复制文件的功能.)如何做到这一点?
您可以使用FileSystemWatcher对象.这会引发指定监视文件夹中文件更改的事件.
class Program { static void Main(string[] args) { FileSystemWatcher fsw = new FileSystemWatcher(@"c:\temp"); fsw.Changed += new FileSystemEventHandler(fsw_Changed); fsw.Deleted += new FileSystemEventHandler(fsw_Deleted); fsw.Renamed += new RenamedEventHandler(fsw_Renamed); fsw.Created += new FileSystemEventHandler(fsw_Created); fsw.EnableRaisingEvents = true; Console.ReadLine(); } static void fsw_Created(object sender, FileSystemEventArgs e) { Console.WriteLine("{0} was created", e.FullPath); } static void fsw_Renamed(object sender, RenamedEventArgs e) { Console.WriteLine("{0} was Renamed", e.FullPath); } static void fsw_Deleted(object sender, FileSystemEventArgs e) { Console.WriteLine("{0} was Deleted", e.FullPath); } static void fsw_Changed(object sender, FileSystemEventArgs e) { Console.WriteLine("{0} was Changed", e.FullPath); } }