You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Electron/Node.js中检测文件或文件夹的打开事件

解决方案

问题根因

fs.watch 底层依赖操作系统原生的文件系统通知接口(Windows为ReadDirectoryChangesW、macOS为FSEvents、Linux为inotify),这类接口本身默认不提供文件/文件夹被打开的事件通知,所以原生fs.watch无法直接捕获打开动作。

可用实现方案

方案1:跨平台通用:结合chokidar监听访问时间(atime)变更

文件被打开时,绝大多数操作系统默认会更新文件的访问时间(atime),你可以通过监听atime的变化来判断文件是否被打开,推荐使用封装了底层事件的第三方库chokidar实现:

  1. 安装依赖:
npm install chokidar
  1. 示例代码:
const chokidar = require('chokidar');

const watcher = chokidar.watch('C:/FileStorage', {
  persistent: true,
  ignorePermissionErrors: true,
  alwaysStat: true // 强制每次事件返回文件stat信息
});

watcher.on('change', (path, stats) => {
  // 仅访问时间变化、修改/创建时间无变化时,判定为文件被打开
  if (stats && stats.atimeMs !== stats.mtimeMs && stats.mtimeMs === stats.ctimeMs) {
    console.log(`文件/文件夹被打开:${path}`);
    console.log(`打开时间:${new Date(stats.atimeMs)}`);
  }
});

watcher.on('error', error => console.log(`监听错误:${error}`));

注意:如果Windows系统关闭了NTFS的atime更新、Linux分区挂载时使用了noatime参数,会导致该方案失效,需要先修改对应系统配置开启atime更新。

方案2:Windows平台专用:调用Win32 API精准监听

如果仅需在Windows环境运行,可以直接调用Win32 API的FindFirstChangeNotification接口,配置FILE_NOTIFY_CHANGE_LAST_ACCESS参数监听访问动作,不受atime配置影响,可通过node-win32-api库实现调用:

// 需提前安装node-win32-api相关依赖
const { Win32Api } = require('node-win32-api');
// 配置监听参数:监听目标目录及子目录的访问时间变更(对应文件打开动作)
const hChange = Win32Api.kernel32.FindFirstChangeNotificationW("C:\\FileStorage", true, 0x00000020);
// 后续轮询处理通知即可

方案3:Linux平台专用:监听inotify原生IN_OPEN事件

Linux的inotify接口原生支持IN_OPEN打开事件,可直接通过inotifyNode库监听,结果最精准:

  1. 安装依赖:
npm install inotify
  1. 示例代码:
const Inotify = require('inotify').Inotify;
const inotify = new Inotify();

const watchDescriptor = inotify.addWatch({
  path: '/home/xxx/FileStorage', // 替换为你的监听目录
  watch_for: Inotify.IN_OPEN, // 直接监听文件打开事件
  callback: (event) => {
    console.log(`文件被打开:${event.name}`);
  }
});

内容的提问来源于stack exchange,提问作者Vahan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 07:39:04