How to detect new or modified files

前端 未结 1 545
礼貌的吻别
礼貌的吻别 2021-01-03 16:01

I found there are two ways to approach it, ReadDirectoryChangesW and FindFirstChangeNotification.

I want to know what’s the difference between them, performance or

相关标签:
1条回答
  • 2021-01-03 16:36

    This is tagged for Winapi, but I give it a shot with .net anyway.

    You can use the FileSystemWatcher class from System.IO for this.

    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);
    }
    
    0 讨论(0)
提交回复
热议问题