Solved

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

Posted on 1998-09-21
3
182 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
indy tidtcpclient threading issue 8 54
PDF library for Delphi 2 89
Delphi cmd execution 6 40
Magic Software info 18 101
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now