Checking If File Is In Use

CK CK 3788 
Created at
Updated at  
921 0 0

IsFileInUse will return true if the file is locked for exclusive access. It would fail if the file doesn't exist at all. 
 

function IsFileInUse(fName : string) : boolean;
var
    HFileRes : HFILE;
begin
    Result := false;
    if not FileExists(fName) then exit;
    HFileRes :=
      CreateFile(pchar(fName),
                 GENERIC_READ or GENERIC_WRITE,
                 0, nil, OPEN_EXISTING,
                 FILE_ATTRIBUTE_NORMAL,
                 0) ;
    Result := (HFileRes = INVALID_HANDLE_VALUE) ;
    if not Result then
    CloseHandle
end;


 

Tags FILE_ATTRIBUTE_NORMAL File I/O Status Check File System Control File is in use GENERIC_READ GENERIC_WRITE OPEN_EXISTING UNC Path Facebook X
Comments 0
Similar posts
  1. Convert a mapped drive to a full UNC path
    832
  2. Does my CD-ROM drive contain an audio CD?
    364
  3. Delete files with the ability to UNDO
    439
  4. Delete folders recursively
    423
  5. From/to the 8.3 (short) format to/from the long format
    579
  6. Get File 'Last Modified' attribute
    465
  7. How to Split and Merge Files
    618
  8. Path shortener: c:\ABC...DEF.ghi
    441
  9. Reading a directory content
    441
  1. How to convert an ICO to a BMP
    361
  2. How to Convert Pixels to Millimeters
    622
  3. How to capture Windows Desktop to Bitmap
    560
  4. Cut a rectangle from an Image to Clipboard
    537
  5. Convert TColor to Hex & Hex to TColor
    590
  6. Convert a BMP to a JPG
    408
  7. Reading a directory content
    441
  8. Path shortener: c:\ABC...DEF.ghi
    441
  9. How to Split and Merge Files
    618
  10. Get File 'Last Modified' attribute
    465
  11. From/to the 8.3 (short) format to/from the long format
    579
  12. Delete folders recursively
    423
  13. Delete files with the ability to UNDO
    439
  14. Does my CD-ROM drive contain an audio CD?
    364
  15. Convert a mapped drive to a full UNC path
    832
  16. Browse for Computers, Folders, Files and Printers
    571
  17. How to draw Transparent Text on bitmap
    386
  18. How Do I Remove The Application Icon From The Taskbar?
    1,098
  19. How To Show The Print Dialog And Print Text Files
    1,295
  20. How To Make An Animated Application Icon
    820
  21. How To Pass The Focus To The Next/prior Control
    1,209
  22. How To Make Rounded Windows
    1,227
  23. How To Get Windows Uptime
    1,476
  24. How To Find Out Total And Available Memory
    1,637
  25. How To Change The System Time
    1,253
  26. How To Get The Windows OS Version
    618
  27. How To Change The Desktop Wallpaper
    2,145
  28. How To Get The Windows Language
    1,145
  29. How To Make A Gradient Filled Form
    839
  30. How can I hide the caption bar on a form?
    1,398
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    39
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    582
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    338
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    216
  30. Understanding Data Lineage
    260