我知道Delphi 7,AnsiString类型是1个字节,但在Delphi 2010中,UnicodeString类型是2个字节。
以下代码适用于Delphi 7
var
FS: TFileStream;
BinarySize: integer;
FreeAvailable, TotalSpace: int64;
fname: string;
StringAsBytes: array of Byte;
begin
..............
FS := TFileStream.Create(drive+'\'+fname+'.BIN', fmOpenReadWrite or fmShareDenyWrite);
try
BinarySize := (Length(fname) + 1) * SizeOf(Char);
SetLength(StringAsBytes, BinarySize);
Move(fname[1], StringAsBytes[0], BinarySize);
FS.Position:=172903;
FS.WriteBuffer(StringAsBytes[0], Length(StringAsBytes));
FS.Position:=173111;
FS.WriteBuffer(StringAsBytes[0], Length(StringAsBytes));
FS.Position:=173235;
FS.WriteBuffer(StringAsBytes[0], Length(StringAsBytes));
FS.Position:=173683;
FS.WriteBuffer(StringAsBytes[0], Length(StringAsBytes));
FS.Position:=173695;
FS.WriteBuffer(StringAsBytes[0], Length(StringAsBytes));
finally
FS.Free;
end;
end;
但它对Delphi 2010不起作用。请帮助!
答案 0 :(得分:0)
如果您不需要支持Unicode-Chars,我会更改它以便明确使用AnsiChar / AnsiString:
var
FS: TFileStream;
BinarySize: integer;
FreeAvailable, TotalSpace: int64;
fname: AnsiString;
StringAsBytes: array of Byte;
begin
..............
FS := TFileStream.Create(drive+'\'+fname+'.BIN', fmOpenReadWrite or fmShareDenyWrite);
try
BinarySize := (Length(fname) + 1) * SizeOf(AnsiChar);
SetLength(StringAsBytes, BinarySize);
Move(fname[1], StringAsBytes[0], BinarySize);
FS.Position:=172903;
FS.WriteBuffer(StringAsBytes[0], Length(StringAsBytes));
FS.Position:=173111;
FS.WriteBuffer(StringAsBytes[0], Length(StringAsBytes));
FS.Position:=173235;
FS.WriteBuffer(StringAsBytes[0], Length(StringAsBytes));
FS.Position:=173683;
FS.WriteBuffer(StringAsBytes[0], Length(StringAsBytes));
FS.Position:=173695;
FS.WriteBuffer(StringAsBytes[0], Length(StringAsBytes));
finally
FS.Free;
end;
end;