如何检查网络共享EXE文件是否由多个用户使用? [英] How to check if network shared EXE file is used by more than one user?

查看:207
本文介绍了如何检查网络共享EXE文件是否由多个用户使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序位于网络共享文件夹中的服务器上.网络中的许多用户可以同时使用该文件.我想对该EXE文件进行更新,并执行此操作,我想知道网络中谁(除我以外)当前正在使用该文件.

I have an application which sits on a server in the network shared folder. Many users in network, can use that file simultaneously. I would like to perform an update of that EXE file and to do this, I would like to know who in network (besides me) is currently using that file.

是否可以通过编程方式进行检查?例如:要列出现在正在使用该文件的所有用户名?还是至少要获取该文件上的锁数量?

Is it possible to check this programmatically? For example: to list all user names who are using that file now? Or atleast to retrieve number of locks on that file?

谢谢.

推荐答案

要列出计算机中打开的共享文件,可以使用ADSI(Active Directory服务接口).

To list the open shared files in a machine you can use ADSI (Active Directory Service Interfaces).

要使用delphi中的这些接口,必须导入Active DS类型库

In order to use these interfaces from delphi you must import the Active DS type library

然后访问 IADsFileServiceOperations 接口,其中包含名为 Resources 此方法返回打开了所有共享资源的集合.

Then access the IADsFileServiceOperations interface, which contains a method called Resources this method return a collection with all the shared resources opened.

检查此示例代码

{$APPTYPE CONSOLE}

uses
  ActiveDs_TLB,
  Variants,
  ActiveX,
  SysUtils;


function ADsGetObject(lpszPathName:WideString; const riid:TGUID; out ppObject):HRESULT; safecall; external 'activeds.dll';


procedure ListSharedResourcesInUse;
var
  FSO           : IADsFileServiceOperations;
  Resources     : IADsCollection;
  Resource      : OleVariant;
  pceltFetched  : Cardinal;
  oEnum         : IEnumvariant;
begin
  //establish the connection to ADSI
  ADsGetObject('WinNT://./lanmanserver', IADsFileServiceOperations, FSO);
  //get the resources interface 
  Resources := FSO.Resources;
  //get the enumerator
  oEnum:= IUnknown(Resources._NewEnum) as IEnumVariant;
  while oEnum.Next(1, Resource, pceltFetched) = 0 do
  begin
    Writeln(Format('Resource %s User %s',[Resource.Path,Resource.User]));
    Resource:=Unassigned;
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      ListSharedResourcesInUse;
    finally
      CoUninitialize;
    end;
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.

这篇关于如何检查网络共享EXE文件是否由多个用户使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆