怎样锁定一个文件?(50分)

  • 主题发起人 主题发起人 longlybug
  • 开始时间 开始时间
L

longlybug

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样锁定一个文件,就是等于独占的方式打开一样
但只是做个标记,在别的程序打开这个文件的时候,
就提示:“另一程序正在使用这个文件”,然后就出错退出
具个例子:比如我对d:/readme.txt锁定后,你双击这个readme.txt
就会提示“另一程序正在使用这个文件”,然后你的记事本就自动退出了

怎么用delphi编程实现呢?
 
procedure OpenForShare(const FileName: String);

var
; FileHandle : Integer;
begin
; FileHandle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);
; if FileHandle > 0 then
; ; {valid file handle}
; else
; ; {Open error: FileHandle = negative DOS error code}
end;
const

; fmOpenRead ; ; ; = $0000;
; fmOpenWrite ; ; ;= $0001;
; fmOpenReadWrite ;= $0002;

; fmShareCompat ; ;= $0000;
; fmShareExclusive = $0010;
; fmShareDenyWrite = $0020;
; fmShareDenyRead ;= $0030;
; fmShareDenyNone ;= $0040;

Description

The file open mode constants are used when a file or stream is opened to control how it can be shared.

The TFileStream constructor has a Mode parameter that you can set to one of these constants:

Constant Definition

fmCreate If the file exists, open for write access, otherwise, create a new file. Unlike the other constants, which are declared in the SysUtils unit, this constant is declared in classes.pas.
fmOpenRead Open for read access only.
fmOpenWrite Open for write access only.
fmOpenReadWrite Open for read and write access.
fmShareCompat Compatible with the way FCBs are opened.
fmShareExclusive Read and write access is denied.
fmShareDenyWrite Write access is denied.

fmShareDenyRead Read access is denied.
fmShareDenyNone Allows full access for others.
 
后退
顶部