views:

306

answers:

3

We have a client server software that needs to be updated. I need to check if the file is currently being accessed. Is this possible if so how Delphi code if possible. The only place I can see if the file is open is under the shared folders open files. I have tried this code but just shows that the file is not opened.

function TfrmMain.FileInUse(FileName: string): Boolean;

var H_File : HFILE;
begin
  Result := False;

  if not FileExists(FileName) then
    begin
    showmessage ('Doesnt Exist');
    exit;
    end;
  H_File := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE, 0,
    nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

  Result := (H_File = INVALID_HANDLE_VALUE);
  showmessage('Opened');
  if not Result then
    CloseHandle(H_File);
end;
A: 

There is a great deal of information you can access over the WBEM sub-system provided by Windows. I believe there are good WBEM components out there, but you could also import the "Microsoft WMI Scripting" COM Type Library (though this takes a little work to figure out how it works).

If you query for Win32_ServerConnection objects, you get a list of items currently in use, much like you can view using the 'Computer Management' tool from the Administrative Tools.

Stijn Sanders
I checked out the Win32_ServerConnection objects and it shows how many files are open but not the names of the files.
Jason
A: 

Not necessarily an answer, but I am currently doing something similar - because the main executable might be updated during working hours though I have created a intermediary application that checks to see if a locally cached copy of the file is up to date, I then run this locally cached copy.

MarkRobinson
A: 

I found this similar item, someone proposes to use the NetFileEnum function

Stijn Sanders