admin

[系统应用] delphi 读取只读文件,读取被占用文件的方法

unit Unit1;


interface


uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;


type

  TForm1 = class(TForm)

    btn1: TButton;

    mmo1: TMemo;

    btn2: TButton;

    procedure btn1Click(Sender: TObject);

    procedure FormCreate(Sender: TObject);

    procedure btn2Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;


var

  Form1: TForm1;


implementation


uses

  Types,Math;  //必须引用的两个单元


{$R *.dfm}

//主要读取函数

function FiIeToByteArray(const FileName:string ):TByteDynArray;

const 

  BLOCK_SIZE=1024;

var

  BytesRead,BytesToWrite,Count:integer;

  F:File of Byte;

  pTemp:Pointer;

begin

  AssignFile( F, FileName );

  Reset(F);

  try

    Count := FileSize( F );

    SetLength(Result, Count );

    pTemp := @Result[0];

    BytesRead := BLOCK_SIZE;

    while (BytesRead = BLOCK_SIZE ) do

    begin

       BytesToWrite := Min(Count, BLOCK_SIZE);

       BlockRead(F, pTemp^, BytesToWrite , BytesRead );

       pTemp := Pointer(LongInt(pTemp) + BLOCK_SIZE);

       Count := Count-BytesRead;

    end;

  finally

     CloseFile( F );

  end;

end;



procedure TForm1.btn1Click(Sender: TObject);

var

    f: file of Byte;

    size: Longint;

    S: String;

begin

    AssignFile(f,hint);

    Reset(f);

    size:=FileSize(f);

    S:='当前文件大小: ' + IntToStr(size);

    mmo1.Lines.Add(s);

    CloseFile(f);

end;


procedure TForm1.FormCreate(Sender: TObject);

begin

  hint:=GetCurrentDir+'\proxmark3.log';

end;


procedure TForm1.btn2Click(Sender: TObject);

begin

  mmo1.Lines.Add(string(FiIeToByteArray(Hint)));  //轻松读取文件内容

end;


end.


#1楼
发帖时间:10月前   |   查看数:0   |   回复数:0
游客组