Solved

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

Posted on 1998-09-21
3
184 Views
Last Modified: 2010-04-06
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
Comment
Question by:ripcord
3 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1340520
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
 
LVL 7

Accepted Solution

by:
Motaz earned 50 total points
ID: 1340521
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
 
LVL 2

Expert Comment

by:kjteng
ID: 1340522
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question