Delphi文件大小又回到了Int64?

use*_*616 1 delphi delphi-7

我有一些我需要处理的大文件,并希望向用户指出文件大小,因为处理可能需要很长时间.

我正在使用David Heffernan的功能(大卫感谢大卫)来获得尺寸,它的效果非常好.

function GetFileSize3(const FileName: string): Int64;
var
  fad: TWin32FileAttributeData;
begin
  if not GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, @fad) then
    RaiseLastOSError;
  Int64Rec(Result).Lo := fad.nFileSizeLow;
  Int64Rec(Result).Hi := fad.nFileSizeHigh;
end;
Run Code Online (Sandbox Code Playgroud)

然后我将其转换为字符串并将其存储在StringList中供以后使用.

当我尝试将其转换回Int64值(myInt64:= StrToInt(slSize [j]))时,我收到一个错误,"xxx不是整数"或非常接近该错误的东西.

我想我应该使用带有Filename:String的Record数组; 大小:Int64的; 在Record中而不是使用StringLists.后见之明非常精彩,现在需要重新编写一次使用记录数组.

我需要一个骗子的方法将非常大的StringList值转换回Int64,以便将少量文件放在正常的StrToInt之外(导致错误的函数).

有人想保存我的培根吗?谢谢.

Ken*_*ite 10

StrToInt64改用.(链接是当前文档,但该功能也存在于Delphi 7中,在SysUtils单元中.)

myInt64 := StrToInt64(slSize[j]);
Run Code Online (Sandbox Code Playgroud)

更好的是,首先不要将它存储在字符串中.Int64将其存储在一个,并且只在需要时将其转换为字符串(例如用于在标签中显示).如果您打算将其用作数字,请将其存储为数字.

您始终可以创建一个只包含一个的小类,Int64并将其TStringList.Objects与包含文件名的字符串一起存储,并Objects在需要大小时将其读回.您甚至可以向该小类添加一个属性,以便在需要时处理转换为字符串.

type
  TFileSizeInfo = class(TObject)
  private
    FFileSize: Int64;
    function GetFileSizeAsString: string;
  public
    constructor Create(TheFileSize: Int64);
    property AsInt64: Int64 read FFileSize write FFileSize;
    property AsString: string read GetFileSizeAsString;
  end;

implementation

constructor TFileSizeInfo.Create(TheFileSize: Int64);
begin
  inherited Create;
  FFileSize := TheFileSize;
end;

function TFileSizeInfo.GetFileSizeAsString: string;
begin
  Result := IntToStr(FFileSize);
end;
Run Code Online (Sandbox Code Playgroud)

使用它:

// Add to stringlist
var
  FileSizeInfo: TFileSizeInfo;
begin
  FileSizeInfo := TFileSizeInfo.Create(GetFileSize3(TheFleName);
  slSize.AddObject(TheFileName, FileSizeInfo);
end;

// Reading it back
var
  SizeAsInt64: Int64;
  SizeAsString: string;
begin
  SizeAsInt64 := TFileSizeInfo(slSize.Objects[Index]).AsInt64;
  SizeAsString := TFileSizeInfo(slSize.Objects[Index]).AsString;
end;

// Clearing your `TStringList` and its `Objects`
procedure ClearFileList(TheList: TStringList);
var
  i: Integer;
begin
  for i := 0 to TheList.Count - 1 do
    TheList.Objects[i].Free;
  TheList.Clear;
end;
Run Code Online (Sandbox Code Playgroud)