Determine if a file is open

Posted on 2006-03-28
Last Modified: 2010-04-05
How can I easily determine if a file is already open before I try to open it programatically.

Specifically, my program involves displaying pdf files in Acrobat Reader.  If the user already has one the pdf file open, how can detect that?

Question by:light_bulb
    LVL 18

    Accepted Solution

    Try this:

    function IsFileInUse(FileName: TFileName): Boolean;
      HFileRes: HFILE;
      Result := False;
      if not FileExists(FileName) then Exit;
      HFileRes := CreateFile(PChar(FileName),
                             GENERIC_READ or GENERIC_WRITE,
      Result := (HFileRes = INVALID_HANDLE_VALUE);
      if not Result then
    LVL 27

    Expert Comment

    usual a try to open the file exclusive should tell you if you can do this,
    if you can -> the file is not in use
    if you not can -> the file is use

    guess this does the suggestion of  Johnjces already,
    it could be done with a TFileStream aslo

    meikl ;-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    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…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 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