如何获取所有进程的打开句柄集
我想知道文件是否被任何进程(包括调用者)打开。已经发布了许多建议,这些建议往往围绕着尝试以独占方式打开文件并捕获异常。
在我的例子中,这并没有被证明是一种万无一失的方法。我相信在我的具体情况下,我可以通过扫描所有正在运行的进程的文件类型的所有打开句柄的名称来完成。
I would like to know if a file is open by any process (including the caller) There are a number of suggestions already posted, which tend to revolve around trying to open the file exclusively and catching the exception.
This has not proven to be a fool-proof method in my instance. I beleive in my specific situation I can get by with scanning the names all open handles of type File of all running processes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为仅仅使用 .NET 和 P/Invoke 无法解决这个问题。您将需要访问低级功能。请参阅 AdaraCD 在另一个论坛的该主题中发布的解决方案,该论坛使用驱动程序开发工具包 (DDK):
I don't think that you can solve this problem simply with .NET and P/Invoke. You will need to access low-level functionality. See the solution posted by AdaraCD in this thread in another forum which is using the driver development kit (DDK):
穷人的方法是从 sysinternals 中 shell 到handle.exe。我不知道是否可以分发。它必须以管理员身份运行(我很确定你需要管理员权限才能做你想做的事情)。无论如何,如果您可以对它进行 shell 并将输出重定向到文件或捕获其标准输出,那么您只需解析其输出即可获得您想要的内容。我在互联网上进行了搜索,似乎没有标记 r。曾经将源代码发布给他的实用程序。
如果您有一个分发的应用程序,这显然不是一个好的解决方案,但如果它只是内部的,也许这就足够了。
此致,
大学教师
a poor man's way would be to shell to handle.exe from sysinternals. i don't know if it can be distributed. it must be run as admin (i'm pretty sure you need admin rights to do what you want anyway). anyway, if you can just shell to it and redirect the output to a file or capture its stdout, then you just have to parse its output to get what you want. i did a search in the internet wayback and it doesn't appear that mark r. ever released the source to his utility.
this obviously isn't a good solution if you have an app you distribute, but if its just in-house perhaps this would be good enough.
best regards,
don
我记得代码handle.exe有开源代码,所以你可以使用这个伟大的实用程序作为参考。
As i remember code handle.exe have open sources so you can use this great utility as reference.