C#监控指定目录的文件变化

c#
2015-04-24 0 523
C#监控指定目录的文件变化

要检查的变化,即从目录中删除目录或文件或文件夹、或者添加到一个新的文件或文件夹到一个目录下,我们必须使用FileSystemWatcher类。这个类允许我们通过程序来监控目录的变化。

创建一个新的FileSystemWatcher对象,Path属性指定的目录,并注册创建和删除事件。

并打开EnableRaisingEvents属性设置为true。

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"c:\mydir";
  
// Register for events
watcher.Created += new FileSystemEventHandler(watcher_Changed);
watcher.Deleted += new FileSystemEventHandler(watcher_Changed);
  
// Start Watching
watcher.EnableRaisingEvents = true;
  
// Event Handler
static void watcher_Changed(object sender,FileSystemEventArgs e)
{
    Console.WriteLine("Directory changed({0}): {1}",
        e.ChangeType,
        e.FullPath);
}

遇见资源网 c# C#监控指定目录的文件变化 http://www.ox520.com/11363.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务