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.
Go into Computer Management and select System Tools >> Shared Folders >> Sessions to see who is connected.
Manage access to a file or folder When you select an item in the Shared with list, the Manage Access panel opens: The Links Giving Access section shows the links that have permissions to the file or folder. Click the ellipsis (...) to see the users that the link has been shared with.
In File Explorer, navigate to the shared folder. Right-click the shared folder, and select Properties. In the Properties window, click Security. Within the Group or user names field, you should see everyone who has permissions relating to that folder.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With