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

跟風遠走 提交于 2019-12-24 13:26:20

问题


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;

回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/2088408/programmatically-check-computer-management-shared-folders-open-files-for-a-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!