如何利用控制台应用程序实现对本机文件的后台监控?

1、利用控制台应用程序实现对本机文件的后台监控,当打开文件时出现弹窗提醒
2、源程序,网上下的就算了

windows 下面可以通过apiFindFirstChangeNotificationA进行监控,这个api在Fileapi.h中,这个方法有三个参数。
1:监控路径(char*)
2:是否监控子路径(bool)
3:过滤信号
官方文档

FindFirstChangeNotification 没有办法监控“打开文件”,只能监控到文件的创建、删除,这个API函数在.NET里对应的封装是 FileSystemWatcher
"打开文件"或者确切地说,访问文件,这个需要写文件驱动过滤(ifs),不能简单用C#实现,因为C#不能编写原生的dll

相关参考资料:https://download.csdn.net/download/lnszyd/2134427

不过你要是局限在监控word、excel这种特定的程序的话,倒是有两个简便易行的办法,一个是用vsto编写addins,一个是编写一个程序,替换掉winword.exe excel.exe,把原来的winword和excel改名叫做winword1.exe excel.exe,你这个程序被执行后记录下得到的文件参数,然后再打开真实的word excel软件,从而在用户看来,一样的使用,但是你的程序拦截了一层,实现了对文件打开的兼容。

我是用windows服务实现的对文件夹监控,FileSystemWatcher 可以了解下。
如果对你有用,请给我20C 谢谢

//设置监视事件
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = path;
            watcher.Filter = fileType;
            watcher.Changed += new FileSystemEventHandler(OnProcess);
            watcher.Created += new FileSystemEventHandler(OnProcess);
            watcher.Deleted += new FileSystemEventHandler(OnProcess);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess
                                   | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
            watcher.IncludeSubdirectories = false;
            watcher.EnableRaisingEvents = true;
            WriteLog.WriteMessage("init", "监视目标地址--" + watcher.Path);
//注册监控方法
private static void OnProcess(object source, FileSystemEventArgs e)
        {
            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                OnCreated(source, e);
            }
            else if (e.ChangeType == WatcherChangeTypes.Changed)
            {
                OnChanged(source, e);
            }
            else if (e.ChangeType == WatcherChangeTypes.Deleted)
            {
                OnDeleted(source, e);
            }

        }

        private static void OnCreated(object source, FileSystemEventArgs e)
        {
            try
            {
                WriteLog.WriteMessage("Info", string.Format(pcName + "文件新建事件处理逻辑 |{0} | {1} | {2}", e.ChangeType, e.FullPath, e.Name));
                string jsonStr = string.Empty;
                Thread.Sleep(10000);
                XmlDocument xml = ReadFile.ReadXml(e.FullPath);
                var fpname = pcName.Split('-');
                jsonStr = XMLHelper.XmlToJson(xml, e.Name, fpname[0], fpname[1]);
                //写入临时文本文件
                WriteLog.WriteJson("JsonData", jsonStr, pcName + "----" + e.Name);
            }
            catch (Exception ex)
            {
                WriteLog.WriteErorrLog("codeError", ex);
            }

        }
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            WriteLog.WriteMessage("Info", string.Format(pcName + "文件改变事件处理逻辑 |{0} | {1} | {2}", e.ChangeType, e.FullPath, e.Name));
        }

        private static void OnDeleted(object source, FileSystemEventArgs e)
        {
            WriteLog.WriteMessage("Info", string.Format(pcName + "文件删除事件处理逻辑 |{0} | {1} | {2}", e.ChangeType, e.FullPath, e.Name));
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            WriteLog.WriteMessage("Info", string.Format(pcName + "文件重命名事件处理逻辑 |{0} | {1} | {2}| {3} | {4}", e.ChangeType, e.FullPath, e.Name, e.OldFullPath, e.OldName));
        }