Solved

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

Posted on 1998-09-21
3
183 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 80
how can i search if string exist in array ? 3 49
JAudiorecorder record freezing the app 29 59
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 57
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

20 Experts available now in Live!

Get 1:1 Help Now