var iFileHandle: Integer; iFileLength: Integer; iBytesRead: Integer; Buffer: PChar; i: Integer begin if OpenDialog1.Execute then begin try iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead); iFileLength := FileSeek(iFileHandle,0,2); FileSeek(iFileHandle,0,0); Buffer := PChar(AllocMem(iFileLength + 1)); iBytesRead = FileRead(iFileHandle, Buffer, iFileLength); FileClose(iFileHandle); for i := 0 to iBytesRead-1 do begin StringGrid1.RowCount := StringGrid1.RowCount + 1; StringGrid1.Cells[1,i+1] := Buffer[i]; StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer[i])); end; finally FreeMem(Buffer); end; end; end; ##FileOpen, FileSeek, FileRead Example ----------------------------------------------------------------------------- FileGetAttr 档案属性 ----------------------------------------------------------------------------- Unit SysUtils 函数原型 function FileGetAttr(const FileName: string): Integer; 说明 faReadOnly = $00000001; faHidden = $00000002; faSysFile = $00000004; faVolumeID = $00000008; faDirectory = $00000010; faArchive = $00000020; faAnyFile = $0000003F; 范例 procedure TForm1.Button1Click(Sender: TObject); var S: String; begin S:=IntToStr(FileGetAttr(''''c:\delphi_d\delphi_help1.txt'''')); Label1.Caption := S; end; ----------------------------------------------------------------------------- FileSetAttr 设定档案属性 ----------------------------------------------------------------------------- Unit SysUtils 函数原型 function FileSetAttr(const FileName: string; Attr: Integer): Integer; 说明 设定成功传回0 ----------------------------------------------------------------------------- FindClose 结束FindFirst/FindNext ----------------------------------------------------------------------------- procedure TForm1.Button1Click(Sender: TObject);
var sr: TSearchRec; FileAttrs: Integer; begin StringGrid1.RowCount := 1; if CheckBox1.Checked then FileAttrs := faReadOnly else FileAttrs := 0; if CheckBox2.Checked then FileAttrs := FileAttrs + faHidden; if CheckBox3.Checked then FileAttrs := FileAttrs + faSysFile; if CheckBox4.Checked then FileAttrs := FileAttrs + faVolumeID; if CheckBox5.Checked then
FileAttrs := FileAttrs + faDirectory; if CheckBox6.Checked then FileAttrs := FileAttrs + faArchive; if CheckBox7.Checked then
FileAttrs := FileAttrs + faAnyFile;
if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then
begin with StringGrid1 do begin if (sr.Attr and FileAttrs) = sr.Attr then begin Cells[1,RowCount-1] := sr.Name; Cells[2,RowCount-1] := IntToStr(sr.Size); end; while FindNext(sr) = 0 do begin if (sr.Attr and FileAttrs) = sr.Attr then begin RowCount := RowCount + 1; Cells[1, RowCount-1] := sr.Name;
var sr: TSearchRec; FileAttrs: Integer; begin StringGrid1.RowCount := 1; if CheckBox1.Checked then FileAttrs := faReadOnly else FileAttrs := 0; if CheckBox2.Checked then FileAttrs := FileAttrs + faHidden; if CheckBox3.Checked then FileAttrs := FileAttrs + faSysFile; if CheckBox4.Checked then FileAttrs := FileAttrs + faVolumeID; if CheckBox5.Checked then
FileAttrs := FileAttrs + faDirectory; if CheckBox6.Checked then FileAttrs := FileAttrs + faArchive; if CheckBox7.Checked then
FileAttrs := FileAttrs + faAnyFile;
if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then
begin with StringGrid1 do begin if (sr.Attr and FileAttrs) = sr.Attr then begin Cells[1,RowCount-1] := sr.Name; Cells[2,RowCount-1] := IntToStr(sr.Size); end; while FindNext(sr) = 0 do begin if (sr.Attr and FileAttrs) = sr.Attr then begin RowCount := RowCount + 1; Cells[1, RowCount-1] := sr.Name; Cells[2, RowCount-1] := IntToStr(sr.Size); end; end; FindClose(sr); end; end; end; ##FindFirst, FindNext, FindClose Example ----------------------------------------------------------------------------- FindNext 寻找下一个符合的档案. ----------------------------------------------------------------------------- Unit SysUtils 函数原型 procedure FindClose(var F: TSearchRec); 函数原型 function FindFirst(const Path: string; Attr: Integer; var F: TSearchRec): Integer; 函数原型 function FindNext(var F: TSearchRec): Integer; 说明 成功传回0 范例 var SRec: TSearchRec; procedure TForm1.SearchClick(Sender: TObject); begin FindFirst(''''c:\delphi\bin\*.*'''', faAnyFile, SRec); Label1.Caption := SRec.Name + '''' is '''' + IntToStr(SRec.Size) + '''' bytes in size''''; end;