WPF 手撸插件 二 检测插件文件夹Plugins中文件变化

如何想要实现插件的热加载或卸载,那么我想第一步就是实时监测插件文夹中文件的变化,以便在插件文件夹中内容发生改变后,及时做出加载或卸载的操作。System.IO.FileSystemWatcher 类为可以满足我们所需的功能。

这里我们做个简单的示例,效果如下图。

示例代码如下

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Path = System.IO.Path;

namespace WpfAppFileSystemWatcher
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            StartMonitoring(GetCurrentProgramPath() + "\\Plugins");
        }

        public static string GetCurrentProgramPath()
        {
            // 获取当前执行的程序集
            var assembly = Assembly.GetEntryAssembly();

            // 获取程序集所在的路径
            var assemblyLocation = assembly.Location;

            // 获取程序集所在的目录
            var programPath = Path.GetDirectoryName(assemblyLocation);

            return programPath;
        }

        private FileSystemWatcher _fileWatcher;
        private void StartMonitoring(string directoryPath)
        {
            // 创建 FileSystemWatcher 实例
            _fileWatcher = new FileSystemWatcher();

            // 设置要监视的目录路径
            _fileWatcher.Path = directoryPath;

            // 设置要监视的更改类型(例如:修改、创建、删除)
            _fileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            // 添加事件处理程序
            _fileWatcher.Changed += OnChanged;
            _fileWatcher.Created += OnChanged;
            _fileWatcher.Deleted += OnChanged;

            // 开启事件监听
            _fileWatcher.EnableRaisingEvents = true;

            Debug.WriteLine($"已开始监视文件夹: {directoryPath}");
        }

        private void OnChanged(object source, FileSystemEventArgs e)
        {
            Debug.WriteLine($"文件或文件夹发生变更: {e.FullPath},事件类型: {e.ChangeType}");
        }
        
        public void StopMonitoring()
        {
            if (_fileWatcher != null)
            {
                // 停止引发事件
                _fileWatcher.EnableRaisingEvents = false;

                // 清理资源
                _fileWatcher.Dispose();
                _fileWatcher = null;

                Console.WriteLine("已停止监视文件夹");
            }
        }
    }
}

本文中文件实时监测,参考了这篇文章。

C# 实时监测文件夹变化

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为风而战

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值