The LockFile function locks a region in an open file. Locking a region prevents other processes from accessing the region. BOOL LockFile(    HANDLE hFile, // handle of file to lock 
    DWORD dwFileOffsetLow, // low-order word of lock region offset 
    DWORD dwFileOffsetHigh, // high-order word of lock region offset  
    DWORD nNumberOfBytesToLockLow, // low-order word of length to lock 
    DWORD nNumberOfBytesToLockHigh  // high-order word of length to lock 
   );
 

解决方案 »

  1.   

    这是API函数,使用方法很简单,可以锁定文件的一部分,
         LockFile(文件句柄,起始字节位置(低32位), 起始字节位置(高32位),
                          字节数(低32位),字节数(高32位));锁成功返回非0.
    UnlockFile()解锁,类似
      

  2.   

    用DELPHI封装的 FileOpen() 打开文件;
      

  3.   

    BCB(:)):能给我一个打开文件,然后锁上,然后再关闭文件,然后再解锁的例子吗?急死我了!救救我吧!  
      

  4.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var FileHandle : Integer;
        buf:Array[0..128]of char;
    begin
        FileHandle := FileOpen('c:\bad.dat', fmOpenWrite or fmShareDenyNone);
        repeat
        until LockFile(FileHandle,0,0,128,0);
        FileWrite(FileHandle,buf,128);
        UnlockFile(FileHandle,0,0,128,0);
        FileClose(FileHandle);
    end;end.
    我对delphi只会一点点,对不对就不知了,仅供参考