Delphi 判断文件是否被占用中
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
function IsFileInUse(AName: string): boolean;
var
hFileRes: HFILE;
begin
Result := False;
if not FileExists(AName) then exit;
hFileRes := CreateFile(PChar(AName), GENERIC_READ or GENERIC_WRITE, 0,
nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Result := hFileRes = INVALID_HANDLE_VALUE;
if not Result then
CloseHandle(hFileRes);
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
if IsFileInUse('c:\1.rar') then
caption := '被占用';
end;
end.
800

被折叠的 条评论
为什么被折叠?



