我正在尝试将文件读入 Delphi XE2 中的字节数组。
这是我当前的代码:
function FileToBytes(const AName: string; var Bytes: TBytes): Boolean;
var
Ms: TMemoryStream;
begin
Result := False;
if not FileExists(AName) then
Exit;
Ms := TMemoryStream.Create;
try
Ms.LoadFromFile(AName);
if Ms.Size > 0 then
begin
Ms.Position := 0;
MS.ReadBuffer(Bytes[0], Ms.Size);
Result := True;
end;
finally
Ms.Free;
end;
end;
procedure runFile();
var
Bytes: TBytes;
OpFile: String;
begin
OpFile := 'C:\Users\Kenny\Documents\calc.exe';
Bytes := nil;
if FileToBytes(OpFile, Bytes) then
begin
//do someting with Bytes(array of Byte)
end;
end;
Run Code Online (Sandbox Code Playgroud)
我在这一行收到错误:
MS.ReadBuffer(Bytes[0], Ms.Size);
Run Code Online (Sandbox Code Playgroud)
错误是: …