hfile := CreateFile('C:\TWO.TXT', // open TWO.TXT
GENERIC_WRITE, // open for writing
0, // do not share
0, // no security
OPEN_ALWAYS, // open or create
FILE_FLAG_DELETE_ON_CLOSE,//FILE_ATTRIBUTE_NORMAL, // normal file
0); // no attr. template
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
procedure Button6Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
hfile:THandle;
public
{ Public declarations }
end;
FileHeader = packed record //25 bytes
uid : int64;
mode : array[1..5] of Char; //模式
begin_time : TDateTime; //開始錄影時間
end_time : TDateTime; //結束錄影時間
total_frame : DWord; //總共Frame 0~65535
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function IsFileInUse(FileName: TFileName): Boolean;
var
HFileRes: HFILE;
begin
Result := False;
if not FileExists(FileName) then Exit;
HFileRes := CreateFile(PChar(FileName),
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 TForm1.Button1Click(Sender: TObject);
var
uidHeader : int64;
BytesRead : Cardinal; //讀寫用參數
begin
hfile := CreateFile('C:\TWO.TXT', // open TWO.TXT
GENERIC_READ, // open for READ
0, // do not share
0, // no security
OPEN_ALWAYS, // open or create
FILE_FLAG_DELETE_ON_CLOSE,//FILE_ATTRIBUTE_NORMAL, // normal file
0); // no attr. template
ReadFile(hFile, uidHeader, SizeOf(uidHeader), BytesRead, nil);
if uidHeader=23342542 then
caption :='good';
//ShowMessage(buf);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
caption := booltostr(IsFileInUse('C:\TWO.TXT'));
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
closehandle(hfile);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
a_fo : TSTReam;
h : FileHeader;
begin
a_fo:=TFileStream.Create('C:\TWO.txt',fmCreate);
h.uid := 23342542;
lstrcpy(@h.mode[1], pchar('abcde'));
a_fo.Write(h, sizeof(h));
a_fo.free;
end;
procedure TForm1.Button5Click(Sender: TObject);
var
a_fo : TSTReam;
h : FileHeader;
z : int64;
begin
a_fo:=TFileStream.Create('C:\TWO.txt',fmOpenRead);
a_fo.Position := 0;
a_fo.Read(h, sizeof(h));
z:= h.uid;
a_fo.free;
end;
procedure TForm1.Button6Click(Sender: TObject);
var
uidHeader : int64;
BytesRead, BytesWrite : Cardinal; //讀寫用參數
pBuf:array[0..1024-1]of byte;
nFileSize : integer;
begin
hfile := CreateFile('C:\BT626.pdf', // open TWO.TXT
GENERIC_WRITE, // open for writing
0, // do not share
0, // no security
OPEN_ALWAYS, // open or create
FILE_ATTRIBUTE_NORMAL,//FILE_ATTRIBUTE_NORMAL, // normal file
0); // no attr. template
nFileSize:= GetFileSize(hfile,nil);
caption := inttostr(nFileSize);
ReadFile(hFile, pBuf, nFileSize, BytesRead, nil);
SetFilePointer(hFile,0,nil,FILE_End);
WriteFile(hFile,pBuf,nFileSize,BytesWrite,nil);
closehandle(hfile);
end;
end.
星期三, 3月 18, 2009
createFile鎖定且會closehandle刪除
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言