判断程序文件是否正在使用

function IsFileInUse(FileName: TFileName): Boolean;
var
 HFileRes: HFILE;
begin
 Result := False;
 if not FileExists(FileName) then Exit;
 HFileRes := CreateFile(PChar(FileName),
   GENERIC_READ or GENERIC_WRITE,
   0,
   nil,
   OPEN_EXISTING,
   FILE_ATTRIBUTE_NORMAL,
   0);
 Result := (HFileRes = INVALID_HANDLE_VALUE);
 if not Result then
   CloseHandle(HFileRes);
end;
 
if IsFileInUse('C:\WINDOWS\system32\calc.exe') then
   ShowMessage('File is in use.')
 else
   ShowMessage('File not in use.');

posted @ 2019-05-24 08:45  inocalli  阅读(229)  评论(0编辑  收藏  举报