C# FileSystemWatcher

C# FileSystemWatcher

FileSystemWatcher 常用于文件系统变更的监控,可以监视一个文件夹,当被监视的文件夹发生修改后,大概会触发以下函数:

  1. Created: 当新建文件或者文件夹1. Changed:当文件或者文件夹已经完成修改1. Renamed:当文件或者文件夹完成重命名1. Deleted:当文件或者文件夹被删除1. Error:当变更过程发生错误
    一个简单地文件监控demo ``` using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks;

namespace ConsoleApp1 { class Program { static void Main(string[] args) {

        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"\\10.***.***.**\Others";
        watcher.Created += new FileSystemEventHandler(Created);
        watcher.Deleted += new FileSystemEventHandler(Deleted);
        watcher.Changed += new FileSystemEventHandler(Changed);
        watcher.Renamed += new RenamedEventHandler(Renamed);
        //开启监控
        watcher.EnableRaisingEvents = true;
        Console.ReadKey();

    }

    private static void Created(object sender, FileSystemEventArgs e)
    {<!-- -->
        Console.WriteLine(e.Name+e.FullPath);
    }

    private static void Deleted(object sender, FileSystemEventArgs e)
    {<!-- -->
        Console.WriteLine(e.Name + e.FullPath);
    }

    private static void Changed(object sender, FileSystemEventArgs e)
    {<!-- -->
        Console.WriteLine(e.Name + e.FullPath);
    }


    private static void Renamed(object sender, FileSystemEventArgs e)
    {<!-- -->
        Console.WriteLine(e.Name + e.FullPath);
    }
}

}

```

posted @ 2020-12-25 15:08  不要摸我的腰  阅读(459)  评论(0编辑  收藏  举报