Registriert seit: 2. Mär 2004
5.508 Beiträge
Delphi 5 Professional
|
AW: EXE-Datei überschreiben mit blockwrite?
21. Aug 2012, 16:36
Es ist mal wieder Zeit für die unentbehrlichen Funktionen StringToFile() und FileToString():
Delphi-Quellcode:
procedure StringToFile(const FileName: string; const Contents: AnsiString; append:Boolean=False);
const
SO_END=2;
var
fs: TFileStream;
Len: Integer;
begin
if append and FileExists(filename) then
begin
fs := TFileStream.Create(FileName, fmOpenReadWrite or fmShareDenyWrite);
end
else
begin
fs := TFileStream.Create(FileName, fmCreate);
append := False;
end;
try
if append then
fs.Seek(0, SO_END); // faster than .Position := .Size
Len := Length(Contents);
if Len > 0 then
{$IFDEF CLR}
fs.WriteBuffer(BytesOf(Contents), Len);
{$ELSE}
fs.WriteBuffer(Contents[1], Len);
{$ENDIF CLR}
finally
fs.Free;
end;
end;
function FileToString(const FileName: string): AnsiString;
var
fs: TFileStream;
Len: Integer;
{$IFDEF CLR}
Buf: array of Byte;
{$ENDIF CLR}
begin
fs := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
Len := fs.Size;
SetLength(Result, Len);
if Len > 0 then
{$IFDEF CLR}
begin
SetLength(Buf, Len);
fs.ReadBuffer(Buf, Len);
Result := Buf;
end;
{$ELSE}
fs.ReadBuffer(Result[1], Len);
{$ENDIF CLR}
finally
fs.Free;
end;
end;
Andreas
|
|
Zitat
|