[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

How to tell if a file is Open or is on readonly drive.

I want to be able to check to see if a file is open or will return access denied prior to attempting to delete a file with SHFileOperation. Windows will popup a box and notify me of the access denied, which I want to avoid. Is there an easy to way to check if a file will return access denied prior to calling SHFileOperation?
0
ripcord
Asked:
ripcord
1 Solution
 
JimBob091197Commented:
Hi

You could try to open the file exclusively first.
For example, for 'C:\Test.txt'

var
  f: TFileStream;
begin
  try
    f := TFileStream.Create('C:\Test.txt', fmOpenWrite or fmShareExclusive);
    f.Free;
    ShowMessage('The file can be deleted.');
  except
    ShowMessage('The file is in use by another process and cannot be deleted!');
  end;
end;

Regards,
JB
0
 
MotazCommented:
Hi, you can do that by this function :

function CanDelete(FileName:string):boolean;
var
    f:file;
begin
  AssignFile(f,FileName);
 FileMode:=2;  // Read and write access
 {$i-}
 Reset(f,1);
 CloseFile(f);
 {$i+}
 if IOResult=0 then
   Result:=true     // Can be deleted
  else  
   Result:=false;   // Access denied
end;
 
end;

Motaz from Sudan.
motaz1@yahoo.com
0
 
kjtengCommented:
Another way is call the function
FileOpen(const FileName: string; Mode: Word)
eg.
i :=  FileOpen(sFile, fmOpenWrite or fmShareExclusive);
if the i<0 then
 {Cannot delete}
else
 {fileClose(i)

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now