如何在 C# 中按进程获取打开的文件句柄列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/177146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:44:04  来源:igfitidea点击:

How do I get the list of open file handles by process in C#?

c#.net

提问by Sam Saffron

How do I get the list of open file handles by process id in C#?

如何通过 C# 中的进程 ID 获取打开文件句柄的列表?

I'm interested in digging down and getting the file names as well.

我也有兴趣深入挖掘并获取文件名。

Looking for the programmatic equivalent of what process explorer does.

寻找与流程浏览器功能相同的程序化功能。

Most likely this will require interop.

这很可能需要互操作。

Considering adding a bounty on this, the implementation is nasty complicated.

考虑为此增加赏金,实现非常复杂。

采纳答案by Sam Saffron

Ouch this is going to be hard to do from managed code.

哎呀,这将很难从托管代码中做到。

There is a sample on codeproject

codeproject 上有一个示例

Most of the stuff can be done in interop, but you need a driver to get the filename cause it lives in the kernel's address space. Process Explorer embeds the driver in its resources. Getting this all hooked up from C# and supporting 64bit as well as 32, is going to be a major headache.

大多数事情都可以在互操作中完成,但是您需要一个驱动程序来获取文件名,因为它存在于内核的地址空间中。Process Explorer 将驱动程序嵌入到其资源中。将这一切从 C# 连接起来并支持 64 位和 32 位,将是一个令人头疼的问题。

回答by Mark Cidade

You can P/INVOKE into the NtQuerySystemInformationfunction to query for all handles and then go from there. This Google groups discussionhas details.

您可以 P/INVOKE 进入NtQuerySystemInformation函数以查询所有句柄,然后从那里开始。这个谷歌小组讨论有详细信息。

回答by Mark Cidade

You can also run the command line app, Handle, by Mark Rusinovich, and parse the output.

您还可以运行Mark Rusinovich 编写的命令行应用Handle并解析输出。

回答by mkm

Handle is great program, and the link to codeproject is good.

Handle 是很棒的程序,与 codeproject 的链接很好。

@Brian The reason for the code is that handle.exe is NOT redistributable. Nor do they release their source.

@Brian 代码的原因是 handle.exe 不可再发行。他们也不公布他们的来源。

It looks as if .Net will not easily do this since it appears that an embedded device drive is requried to access the information. This cannot be done in .net without an unmanged DLL. It's relatviely deep kernel code when compared to typical .net coding. I'm surprised that WMI does not expose this.

看起来 .Net 似乎不会轻易做到这一点,因为似乎需要嵌入式设备驱动器来访问信息。如果没有不受管理的 DLL,这无法在 .net 中完成。与典型的 .net 编码相比,它是相对深入的内核代码。我很惊讶 WMI 没有公开这一点。

回答by Kiquenet

Perhaps using command line tool:

也许使用命令行工具:

OpenedFilesView v1.50 - View opened/locked files in your system (sharing violation issues)

OpenedFilesView v1.50 - 查看系统中打开/锁定的文件(共享违规问题)

http://www.nirsoft.net/utils/opened_files_view.html

http://www.nirsoft.net/utils/opened_files_view.html

回答by manuc66

Have a look at this file : http://vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318

看看这个文件:http: //vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318

And use:

并使用:

DetectOpenFiles.GetOpenFilesEnumerator(processID);

Demo:

演示:

using System;
using System.Diagnostics;

namespace OpenFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var openFiles = VmcController.Services.DetectOpenFiles.GetOpenFilesEnumerator(Process.GetCurrentProcess().Id))
            {
                while (openFiles.MoveNext())
                {
                    Console.WriteLine(openFiles.Current);
                }
            }
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}

It has dependency over assembly System.EnterpriseServices

它依赖于程序集 System.EnterpriseServices

回答by user541686

Take a look at wj32's Process Hacker version 1, which can do what you asked, and more.

看看 wj32 的Process Hacker version 1,它可以做你所要求的,等等。