---
title: "判断文件是否使用中 Checking If File Is In Use"
author: "eueuy"
date: "2010-06-11T10:07:10Z"
tags: ["Access", "Delphi", "UP"]
categories: ["编程语言"]
slug: "checking-if-file-is-in-use"
draft: false
lastmod: "2010-06-11T10:07:10Z"
id: 689489
url: "https://qwegqwe.iteye.com/blog/689489"
---

# Checking If File Is In Use

[![Your Guide, Zarko Gajic](http://eueuy.iteye.com/admin/blogs/8e9d22ff105b0560cd6a2bc9d57b8ccd_64_files/delphi%5B1%5D.gif)](http://delphi.about.com/mbiopage.htm) From [Zarko Gajic](http://delphi.about.com/mbiopage.htm),  
Your Guide to [Delphi Programming](http://delphi.about.com/).  
**FREE** Newsletter. [Sign up now!](http://delphi.about.com/gi/pages/mmail.htm)

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  
\~~~~~~~~~~~~~~~~~~~~~~~~~

*Delphi tips navigator:*  
» [Capture Maximize/Minimize Buttons](http://delphi.about.com/cs/adptips1999/a/bltip0999_4.htm)  
« [Rotating text](http://delphi.about.com/cs/adptips1999/a/bltip0999_2.htm)

uses wininet

 

if not InternetCheckConnection(PChar(CUrl), 1, 0) then  
  Begin  
    ShowMessage('网路不通!');  
    Exit;  
  End;