首页 > 解决方案 > C#:查询远程服务器以查看哪个用户锁定了文件

问题描述

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

我已经挖了几个小时了,无法弄清楚...

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

我能想到的最好的方法是

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

但对于我的生活,我无法弄清楚如何IADsFileServiceOperations在.Net 项目中使用。

帮助...?请...?

标签: c#active-directorywmi

解决方案


IADsFileServiceOperations是一个公开两个方法的接口:

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

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

IADsFileServiceOperations::Resources 方法获取一个指针,该指针指向代表此文件服务上当前打开资源的资源对象集合上的 IADsCollection 接口。

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

希望它能解决你的问题。


推荐阅读