文件锁定哪些过程?过程、文件

2023-09-03 06:18:00 作者:剩余酸涩

有没有办法在.net中找出到底是哪个进程锁定的文件?

Is there a way in .Net to find out exactly which process has locked a file?

修改:我这样做是因为我想让我的用户知道他们不能修改/打开该文件,因为此刻,他们正在使用其他程序(如Excel中)已打开。希望这有助于。

EDIT: I'm doing this because I want to let my user know that they can't modify/open the file, because at the moment, another program they're using (such as Excel) has it open. Hopefully, this helps.

推荐答案

简短的答案是否定的。

不过,从长远的答案是,有不同的API调用和WMI方法,你可以用它来寻找这些信息了,但不要指望它是快速和简单。

However, the long answer is that there are various API calls and WMI methods that you can use to find this information out, but don't expect it to be quick and simple.

如果你想使用API​​调用,看看在的 NtQuerySystemInformation 功能与SYSTEM_PROCESS_INFORMATION参数。这是那些可爱的无证的方法之一自带的精彩免责声明:

If you want to use API calls, take a look at the NtQuerySystemInformation function with the SYSTEM_PROCESS_INFORMATION parameter. This is one of those lovely "undocumented" methods that comes with the wonderful disclaimer:

NtQuerySystemInformation可能   改变或未来不可用   Windows版本。应用   应该使用复用功能   本主题中列出。

NtQuerySystemInformation may be altered or unavailable in future versions of Windows. Applications should use the alternate functions listed in this topic.

所以我建议避免在有利于使用WMI的。

So I would suggest avoiding that in favour of using WMI.

您可以使用 WMI的Win32_Process 类枚举计算机上正在运行的所有进程,然后枚举所有处理每个进程正在举行,直到你找到你要找的文件。不幸的是有没有简单的方法去哎,是哪个进程锁定该文件,它只能倒过来,你必须向下搜索进程列表,直到你找到一个被锁定你感兴趣的文件。

You can use the WMI Win32_Process class to enumerate all processes currently running on the machine, and then enumerate all handles each process is holding until you find the file you are looking for. Unfortunatly there is no simple way to go "hey, which process is locking this file", it only works the other way round you have to search down the process list until you find the one that is locking the file you are interested in.

我会建议在$ C $的CProject一个可爱的小文章,题为如何要:(几乎)一切都在WMI通过C#第2部分:过程的。 (第1部分也是很好看的,如果你喜欢之类的话)

I'd recommend a nice little article on CodeProject titled How To: (Almost) Everything In WMI via C# Part 2: Processes. (Part 1 is also a good read if you like that kind of thing)

 
精彩推荐