c# 文件监听

public class FileListenerServer
    {
        private FileSystemWatcher _watcher;
        public FileListenerServer()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path">监听目录</param>
        public FileListenerServer(string path)
        {
            try
            {
                this._watcher = new FileSystemWatcher();
                _watcher.Path = path;//监视目录
                _watcher.Filter = "*.config";//过滤文件
                _watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.DirectoryName;
                _watcher.IncludeSubdirectories = true;//监视子目录

                _watcher.Created += new FileSystemEventHandler(FileWatcher_Created);
                _watcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);
                _watcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);
                _watcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);
            }

            catch (Exception ex)
            {
                Console.WriteLine("Error:" + ex.Message);
            }
        }

        public void Start()
        {
            this._watcher.EnableRaisingEvents = true;
            Console.WriteLine("文件监控已经启动...");
        }

        public void Stop()
        {
            this._watcher.EnableRaisingEvents = false;
            this._watcher.Dispose();
            this._watcher = null;
        }

        protected void FileWatcher_Created(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("新增:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
        }
        protected void FileWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            if (e.Name.Equals("App.config"))
            {
                var configPath = (AppDomain.CurrentDomain.BaseDirectory + "App.config");
                ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                map.ExeConfigFilename = configPath;
                var configuartion = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                var value = configuartion.AppSettings.Settings["key"].Value;
            }
            Console.WriteLine("修改:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
        }
        protected void FileWatcher_Deleted(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("删除:" + e.ChangeType + ";" + e.FullPath + ";" + e.Name);
        }
        protected void FileWatcher_Renamed(object sender, RenamedEventArgs e)
        {
            Console.WriteLine("重命名: OldPath:{0} NewPath:{1} OldFileName{2} NewFileName:{3}", e.OldFullPath, e.FullPath, e.OldName, e.Name);
        }
    }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值