【问题标题】:Checking if a file was opened and return path to opened file (in Windows 7)检查文件是否打开并返回打开文件的路径(在 Windows 7 中)
【发布时间】:2015-05-17 20:58:25
【问题描述】:

我想创建一个应用程序来监控打开的进程。我想用它来监控我打开 mp3 文件的频率。该程序将在后台运行并计算我运行每个 mp3 文件的次数,然后我会根据这个数字对我的 mp3 文件进行排序。

我使用Process Monitor 来检查是否可以完成。当我过滤输出以使其仅显示我的 mp3 播放器进程并且我设置为仅查看“进程启动”操作时,我可以读取打开了哪个文件,例如这是 Process Monitor 显示了实时打开的文件。在每个文件运行后,Process Monitor 中都会有一个新的输入。

如您所见,我可以轻松地计算进程监视器的输出以获取每个文件的启动次数。但是,我不知道它是如何完成的,因为 Process Monitor 是一个 .exe,我看不到代码内部。

解决我的问题的最简单方法是什么?其实我想用的编程语言无所谓,可以是C#、C++、Python。

提前谢谢你。

【问题讨论】:

    标签: windows events monitoring monitor event-log


    【解决方案1】:

    我自己想出了一些东西。在 Python 2.7 中使用这个库 psutil 应该就足够了。我的进程名称将保持不变,因为我总是使用同一个程序来运行我的 mp3/视频文件。

    1. 查找我的进程的 PID
    2. 当我拥有 PID 后,我可以检查我的进程当前打开了哪些文件。
    3. 始终记住上次打开的文件。如果当前文件与上一个不同,则将当前文件的打开次数加 1。这就是我可以根据打开频率创建打开文件列表的方法。
    4. 使用 while 循环在后台检查此内容。

    这里有一些少量的代码,但总的来说灵魂很简单:)

    import psutil
    
    process_list = psutil.pids()
    
    for i in process_list:
        temp_process = psutil.Process(i)
    
        #if process doesn't have a name then the .name() method will cause a crash, we need to try-catch it
        try:
            if (temp_process.name()=="mpc-hc64.exe"):
                print temp_process.cmdline() #in my case opened file will be listed in the cmdline, may not always be true
                print temp_process.open_files() #this however should ALWAYS return files that are opened by current process!!
        except:
            print "no process name"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 2022-12-04
      • 2023-04-10
      • 1970-01-01
      相关资源
      最近更新 更多