zoukankan      html  css  js  c++  java
  • c#如何监视文件或者文件夹的变化

    可以使用FileSystemWatcher类来监视文件或者文件夹的变化。

    下面的代码可以用来监视重命名的行为。

    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = @"C:\MyDirectory";
    watcher.Renamed += new RenamedEventHandler(watcher_Renamed)
    watcher.EnableRaisingEvents = true;

    void watcher_Renamed(object sender, RenamedEventArgs e)
    {
      Console.WriteLine("File Renamed: Old Name: " + e.OldName +
        " New Name: " + e.Name);
    }    
     

    FileSystemWatcher类的其他事件如下:

    watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
    watcher.Changed += new FileSystemEventHandler(watcher_Changed);
    watcher.Created += new FileSystemEventHandler(watcher_Created);

    这些事件都传递一个 FileSystemEventArgs参数,这个参数包含 filename, full path, what action caused the event 等信息.

  • 相关阅读:
    正则表达式复习 (?<=) (?=)
    HTML 30分钟入门教程
    C# 多线程详解
    C# List
    C# 枚举
    C# 线程数
    C# 泛型2
    C# 泛型
    C# 结构体
    不用Google Adsense的84个赚钱方法
  • 原文地址:https://www.cnblogs.com/pdfw/p/1667209.html
Copyright © 2011-2022 走看看