Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1998-09-21
3
Medium Priority
?
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

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.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

636 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