当前位置:  开发笔记 > 编程语言 > 正文

如何检测新文件或修改过的文件

如何解决《如何检测新文件或修改过的文件》经验,为你挑选了1个好方法。

我发现有两种方法可以解决它,ReadDirectoryChangesW以及FindFirstChangeNotification.

我想知道它们,性能或其他方面有什么区别?



1> Oliver Fried..:

这是为Winapi标记的,但无论如何我用.net给它一个镜头.

您可以使用System.IO中的FileSystemWatcher类来实现此目的.

static void Main(string[] args)
{
    FileSystemWatcher fsw = new FileSystemWatcher("C:\\");
    fsw.Changed += new FileSystemEventHandler(fsw_Changed);
    fsw.Created += new FileSystemEventHandler(fsw_Created);
    fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
    fsw.EnableRaisingEvents = true;
    fsw.IncludeSubdirectories = true;
    fsw.Renamed += new RenamedEventHandler(fsw_Renamed);

    Console.Read();
}

static void fsw_Renamed(object sender, RenamedEventArgs e)
{
    Console.WriteLine("{0} was changed to {1}", e.OldName, e.Name);
}

static void fsw_Deleted(object sender, FileSystemEventArgs e)
{
    Console.WriteLine("{0} was deleted", e.Name);
}

static void fsw_Created(object sender, FileSystemEventArgs e)
{
    Console.WriteLine("{0} was created", e.Name);
}

static void fsw_Changed(object sender, FileSystemEventArgs e)
{
    Console.WriteLine("{0} was changed", e.Name);
}

推荐阅读
wangtao
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有