开发者

programmatically check computer management - shared folders - open files for a file

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 f开发者_JAVA百科iles. 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;


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜