如何在Windows 7中监视文件启动 [英] How to monitor files startups in Windows 7

查看:119
本文介绍了如何在Windows 7中监视文件启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我用进程监视器检查是否可以完成。当我过滤输出,使它只显示我的MP3播放器进程,并设置为只看到处理开始操作,然后我可以读取打开的文件,例如这是进程监视器<的命令行详细信息/ b>向我展示了实时开放的文件。每个文件运行后,Process Monitor中都有一个新输入。

查看截图以查看我在说什么





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



什么是最简单的方法解决我的问题?实际上我想使用的编程语言并不重要,可以是C#,C ++,Python。



提前谢谢。

Hi. I would like to create an application that would monitor processes that are opened. I want to use it to monitor how often do I open my mp3 files. The program would run in background and count the number of times I run each mp3 file and later I would sort my mp3 files based on this number.

I used Process Monitor to check if it can be done. When i filter the output so that it shows only my mp3 player processes and i set to see only "Process Start" operation then I can read which file was opened, for example this is the command line detail that the Process Monitor showed me files that are opend in real-time. After each file run there is a new input in Process Monitor.
See screenshot to look what I am talking about:


As you can see I could easily count the Process Monitor outputs to get the number of times each file was started. However, I don't know how it is done because Process Monitor is an .exe and i can't see inside the code.

What is the easiest way to solve my problem? In fact programming language I would like to use doesn't matter, can be C#, C++, Python.

Thank you in advance.

推荐答案

没有简单的方法来做到这一点。您必须每隔一段时间轮询MP3播放器进程的句柄表以查看它已打开的文件。



这个 [ ^ ]是关于Process Monitor如何做的一点提示。
There is no "easy" way to do this. You'd have to poll the handle table of your MP3 player process every once in a while to see which files it has open.

This[^] is a little hint as to how Process Monitor does it.


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



1.查找我的进程的PID

2.当我有PID时,我可以检查我的进程当前打开了哪些文件。

3.永远记住上次打开的文件。如果当前文件与上一个文件不同,则将当前文件的打开计数增加1.

4.使用while循环在后台检查。



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



I have come up with something on my own. It should be enough to use this library psutil in Python 2.7. My process name will be constant, because I always use the same program to run my mp3 / video files.

1. Find PID of my process
2. When I have the PID i can check what files are currently opened by my process.
3. Always remember last opened file. If the current file is different from last one, then increment current's file open count by 1.
4. Use while loop to check this in background.

Here's some small amount of code but in general the soultion is quite simple :)

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"


这篇关于如何在Windows 7中监视文件启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆