[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

What is holding a file?

Posted on 2004-11-21
2
Medium Priority
?
254 Views
Last Modified: 2010-04-04
Dear all,

If a doc file is in use by a program say MS Word, when I try to delete this file in Windows Explorer, I will get a message that this file is in use by Word like the screen below:

http://www.creightonbrown.geek.nz/Docs/ComputerSupport/Cannot%20Delete%20file.gif

How do I detect which program is holding a file in Delphi?

Regards,
Fox
0
Comment
Question by:foxvision
2 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12645407
Well, it would be a difficult technique, where you have to enumerate all processes and then all filehandles within each process. Based upon the filehandle you must check to see if it's attached to the file you want to delete. If it is, then you've found one process that keeps it locked. (But there could be more!)
See http://www.sysinternals.com/ntw2k/freeware/handle.shtml for a tool that just shows which files are open by which process.

Unfortunately, there doesn't seem to be any source available and I don't have any useful source either...
0
 
LVL 2

Accepted Solution

by:
sdmason earned 500 total points
ID: 12677192
I've got a partial solution (I can't see all processes) using Colin Wilson's code found at
http://www.wilsonc.demon.co.uk/d7fileanalyzer.htm
To build the application you also need to get his NT Low level utilities from
http://www.wilsonc.demon.co.uk/d7ntutils.htm
and the misc components.
http://www.wilsonc.demon.co.uk/d7miscunits.htm

// Function
uses
  NTDDK,
  unitNTHandles;

function FileUsedBy(const AFileName : TFileName; var UsedBy : TStringList) : integer;
var
  ASHandles                             : TSystemHandles;
  AFHandles                             : TSystemFileHandles;
  liIndex                               : integer;
  fhiTest                               : TFileHandleInfo;
begin
  Result := 0;
  UsedBy.Clear;

  ASHandles := TSystemHandles.Create;
  AFHandles := TSystemFileHandles.Create(ASHandles);
  try

    for liIndex := 0 to AFHandles.Count - 1 do
      begin
        fhiTest := AFHandles.FileHandle[liIndex];
        if Pos(AFileName, fhiTest.FileName) <> 0 then  // You may want to match the whole path or part of a path
          begin
            Inc(Result);
            UsedBy.Add(fhiTest.ProcessName);
          end;
      end;
  finally
    AFHandles.Free;
    ASHandles.Free;
  end;
end;

// Sample usage

procedure TForm1.btnTestClick(Sender : TObject);
var
  lsTestFile                            : string;
  lsUser                                : TStringList;
begin
  lsTestFile := 'source';
  lsUser := TStringList.Create;

  if FileUsedBy(lsTestFile, lsUser) <> 0 then
    ShowMessage('Used by ' +#13#10+  lsUser.Text);
  lsUser.Free;
end;

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

834 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