Mega Code Archive

 
Categories / Delphi / Files
 

How can I know an application file whether it is in use or not

Title: How can I know an application file whether it is in use or not? Question: is an application in use? Answer: Within a program, you may want to know an application file whether it is in use or not.Then you can use the following function. Function ApplicationUse(fName : string ) : boolean; var HFileRes : HFILE; begin Result := false; if not FileExists(fName) then exit; HFileRes := CreateFile(pchar(fName), 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; This is an example of using this function: procedure TForm1.Button1Click(Sender: TObject); begin if ApplicationUse('c:\project1.exe') then ShowMessage('Application in use') else ShowMessage('Application not in use'); end;