zoukankan      html  css  js  c++  java
  • 利用windows系统自带的API检测文件的修改/删除/新建/重命名

    这个接口是比较好用的,不需要第三方的dll,只需要system.IO引入了就可以使用了

    修改/删除/新建/重命名这几种对文件的操作都能够被监测到

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Configuration;
    
    namespace ReadExcel.Common
    {
        class FileWatcher
        {
            public void Watcher()
            {
    
                FileSystemWatcher watcher = new FileSystemWatcher();
    
                watcher.Path = ConfigurationManager.AppSetting("Detach");
    
                watcher.NotifyFilter = NotifyFilters.LastAccess
                                     | NotifyFilters.LastWrite
                                     | NotifyFilters.FileName
                                     | NotifyFilters.DirectoryName;
    
                watcher.Filter = "*";
    
                // Add event handlers.
                watcher.Changed += new FileSystemEventHandler(this.OnChanged);
                watcher.Created += new FileSystemEventHandler(this.OnChanged);
                watcher.Deleted += new FileSystemEventHandler(this.OnChanged);
                watcher.Renamed += new RenamedEventHandler(this.OnRenamed);
    
                // Begin watching.
                watcher.EnableRaisingEvents = true;
    
            }
            private void OnChanged(object source, FileSystemEventArgs e) =>
                Console.WriteLine($"File: {e.FullPath} {e.ChangeType}");
    
            private void OnRenamed(object source, RenamedEventArgs e) =>
                Console.WriteLine($"File: {e.OldFullPath} renamed to {e.FullPath}");
        }
    
    
    }
  • 相关阅读:
    没有完成的题目
    哈尔滨工程大学 ACM online contest 1008 how many
    POJ 2976 分数规划
    长沙理工 ACM 数位 DP 1488
    POJ 2663
    USETC 1821 AC 自动机
    长沙理工 ACM 分数规划 1494
    正则表达式基础知识(转)
    上传头像代码
    datalist 分页(转)
  • 原文地址:https://www.cnblogs.com/yinxuejunfeng/p/14194654.html
Copyright © 2011-2022 走看看