{
    FileSystemWatcher watcher = new FileSystemWatcher
    {
        Path = @"D:\workspace",
        NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite,
        Filter = "*.*",
        IncludeSubdirectories = true
    };

    watcher.Changed += OnChanged;
    watcher.Created += OnChanged;
    watcher.Deleted += OnChanged;
    watcher.Renamed += OnRenamed;

    watcher.EnableRaisingEvents = true;
}

// File system change handler
private void OnChanged(object source, FileSystemEventArgs e)
{
    UpdateDatabase(e.FullPath, e.ChangeType);
}

private void OnRenamed(object source, RenamedEventArgs e)
{
    UpdateDatabase(e.OldFullPath, WatcherChangeTypes.Deleted);
    UpdateDatabase(e.FullPath, WatcherChangeTypes.Created);
最后修改:2024 年 09 月 29 日
如果觉得我的文章对你有用,请随意赞赏