C#:查询远程服务器以查看哪个用户锁定了文件

C#: Querying a remote server to see which user has a lock on a file

** 编辑:原来的问题不清楚。我无法在任何 .Net 程序集中找到 IADsFileServiceOperations,这就是问题所在 - 并不是我不知道如何使用它 **

我已经研究了几个小时,还是想不通...

在 C# 中,有没有办法查询远程文件服务器(最好使用 ManagementObjectSearcher 但不一定)并询问哪个用户当前锁定了文件?

我能想到的最好的是

var en = new DirectoryEntry($@"\{hostName}\root\cimv2");
var fso = sessQuery.NativeObject as IADsFileServiceOperations;
var resources = fso.Resources();

但对于我来说,我不知道如何在 .Net 项目中使用 IADsFileServiceOperations

求助...?请问...?

IADsFileServiceOperations是一个接口,暴露了两个方法:

  1. IADsFileServiceOperations::资源
  2. IADsFileServiceOperations::会话

https://docs.microsoft.com/en-us/windows/desktop/api/iads/nn-iads-iadsfileserviceoperations#methods

The IADsFileServiceOperations::Resources method gets a pointer to a pointer to the IADsCollection interface on a collection of the resource objects representing the current open resources on this file service.

var en = new DirectoryEntry($@"\{hostName}\root\cimv2");
var fso = sessQuery.NativeObject as IADsFileServiceOperations;
var resources = fso.Resources(); // returns resource objects representing the current open resources on the file

foreach(var res in resources)
{
   // res.User
   // res.Path
   // res.LockCount
   if(res.LockCount > 0)
   {
      // user has lock on file
   }
}

更新 1:

要向您的 .NET 项目添加活动目录类型,您需要向该项目添加 COM 引用。转到添加引用对话框,然后选择 COM 选项卡并添加 Active DS Type Library.

通过添加此 COM 引用,您拥有所有 AD 类型,使用 ActiveDS.

的接口

希望它能解决您的问题。