如何检查网络共享EXE文件是否被多个用户使用?

发布于 2024-11-19 13:48:05 字数 167 浏览 3 评论 0原文

我有一个应用程序位于网络共享文件夹中的服务器上。网络中的许多用户可以同时使用该文件。我想执行该 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?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃不饱 2024-11-26 13:48:05

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

为了从 delphi 使用这些接口,您必须导入 Active DS 类型库

在此处输入图像描述

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

检查这个示例代码

{$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.

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

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

enter image description here

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

Check this sample code

{$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.
帅冕 2024-11-26 13:48:05

如果您真正需要的是替换可能正在使用的 exe 文件,您可以重命名可执行文件,然后将新文件复制到同一位置。新用户将运行新的可执行文件,而旧的(重命名的)可执行文件可以在最后一个运行它的用户关闭应用程序后立即删除。

If what you really need is to replace an exe file, that may be in use, you can rename the executable, and then just copy the new file on the same location. New users will run the new executable, where as the old (renamed) one can be deleted as soon as the last user, running it, closes the application.

相权↑美人 2024-11-26 13:48:05

这可能有点过分了,但是拥有服务器-客户端架构怎么样?应用程序可能正在您的服务器上运行,每次有人启动客户端时,它都会连接到服务器。

在方程式中添加一个“状态/状态”,可以根据您当前的需要进行更改:正常、准备升级、升级进行中等。

这样您就可以遍历打开的连接列表,并向每个人发送一条消息,告知系统将置于维护模式,或者当客户端启动时,它可以通知用户发生了什么。

干杯

It might be overkill, but what about having a server-client architecture? An app could be running on your server, and each time someone launches a client, it connects to the server.

Add a "status/state" to the equation which can change to your current need: NORMAL, PREPARING FOR UPGRADE, UPGRADE IN PROGRESS, etc

This way you could iterate through the list of opened connections and send a message to everyone that the system will be put in maintenance mode, or when the client gets launched it can notify the user of what's going on.

Cheers

热血少△年 2024-11-26 13:48:05

如果您启动 exe,它将被锁定以进行写入。因此,替换或重命名 exe 是安全的,但如果不能,则意味着该 exe 已被使用。

PsFile 和“net file”可以显示文件是否打开并且可以关闭文件。但这对于已启动程序的用户来说并不好。

我认为最好创建向程序的所有用户发送消息的功能。如果您需要升级,请禁用新登录并向所有用户发送消息“请退出,需要升级!”。重试此操作,直到可以替换 exe。 PsFile 无法判断谁在使用文件,但只能判断文件是否被使用。

If you start exe it will be locked for write. So it is safe to replace or rename exe, but if you can not this mean that exe is used.

PsFile and "net file" can show if file is opened and you can close file. But this is not good for users that are started program.

I think it is best if you create function to send message to all users of program. If you need upgrade disable new logins and send message to all users "Please exit, upgrade is required!". Retry this until you can replace exe. PsFile can not tell who is using file but only if file is used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文