...check if a file can be opened?

Author: Thomas Stutz

Category: Files

function IsFileInUse(const fName: TFileName): Boolean;
var
  
HFileRes: HFILE;
begin
  
Result := False;
  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;


procedure TForm1.Button1Click(Sender: TObject);
begin
  if 
OpenDialog1.Execute then
    if 
IsFileInUse(Opendialog1.FileName) then
      
ShowMessage('File is in use!');
end;




 

printed from
www.swissdelphicenter.ch
developers knowledge base