So müsste es gehn.
Komponenten:
2 Edits, 2 Buttons, OpenDialog, SaveDialog
Edit1 - Eingabe der Zahl die gespeichert werden soll.
Edit2 - Ausgabe der Zahl die gespeichert wurde.
LadenButton - Eine EXE Laden und Zahl auslesen.
SpeichernButton - Eine neue EXE mit einer Zahl speichern.
Code:
Delphi-Quellcode:
private
function PrepareConfigFile: TStrings;
function PrepareFile(aFileName, aNewFileName: String; aConfigFileText: TStrings): Boolean;
function LoadConfigFile(aFileName: String): Boolean;
end;
uses
IniFiles;
function CreateConfigFile: TStrings;
var
ConfigFile: TStrings;
begin
ConfigFile := TStringList.Create;
ConfigFile.Add('ZAHL=');
Result := ConfigFile;
ConfigFile.Free;
end;
function TForm1.PrepareConfigFile: TStrings;
var
ConfigFile: TStrings;
begin
ConfigFile := TStringList.Create;
ConfigFile.AddStrings(CreateConfigFile);
ConfigFile.Strings[0] := ConfigFile.Strings[0] + Edit1.Text;
Result := ConfigFile;
ConfigFile.Free;
end;
function TForm1.PrepareFile(aFileName, aNewFileName: String; aConfigFileText: TStrings): Boolean;
var
OrigFile, ConfigFile, NewFile: TFileStream;
ConfigFileText: TStrings;
OrigFileSize, ConfigFileSize: Integer;
begin
NewFile := TFileStream.Create(aNewFileName, fmOpenWrite);
OrigFileSize := SelfSize;
NewFile.Seek(OrigFileSize, soFromBeginning);
SetEndOfFile(NewFile.Handle);
ConfigFileText := TStringList.Create;
ConfigFileText.Assign(aConfigFileText);
ConfigFileText.Insert(0, 'SELF=' + IntToStr(OrigFileSize));
ConfigFileText.SaveToFile(ExtractFilePath(aNewFileName) + 'TempFile.conf');
ConfigFileText.Free;
ConfigFile := TFileStream.Create(ExtractFilePath(aNewFileName) + 'TempFile.conf', fmOpenRead);
ConfigFileSize := ConfigFile.Size;
NewFile.CopyFrom(ConfigFile, 0);
NewFile.Write(ConfigFileSize, 4);
ConfigFile.Free;
NewFile.Free;
DeleteFile(ExtractFilePath(aNewFileName) + 'TempFile.conf');
Result := True;
end;
function TForm1.LoadConfigFile(aFileName: String): Boolean;
var
aFile, ConfigFile: TFileStream;
ConfigFileText: TStrings;
ConfigFileSize: Integer;
TempStr: String;
begin
aFile := TFileStream.Create(aFileName, fmShareExclusive);
ConfigFile := TFileStream.Create('File.conf', fmCreate);
aFile.Seek(-4, soFromEnd);
aFile.Read(ConfigFileSize, 4);
aFile.Seek(-4 - ConfigFileSize, soFromEnd);
ConfigFile.CopyFrom(aFile, ConfigFileSize);
ConfigFile.Free;
aFile.Free;
ConfigFileText := TStringList.Create;
ConfigFileText.LoadFromFile('File.conf');
DeleteFile('File.conf');
if Pos('SELF=', ConfigFileText[0]) > 0 then
begin
TempStr := ConfigFileText[0];
Delete(TempStr, Pos('SELF=', TempStr), 5);
SelfSize := StrToInt(TempStr);
end;
ConfigFileText.Delete(0);
// Daten auslesen
Edit2.Text := Copy(ConfigFileText[0], 4, Length(ConfigFileText[0]));
ConfigFileText.Delete(0);
ConfigFileText.Free;
Result := True;
end;
procedure TForm1.SpeichernButtonClick(Sender: TObject);
var
ConfigFile: TStrings;
begin
if SaveDialog1.Execute then
begin
ConfigFile := TStringList.Create;
ConfigFile.AddStrings(PrepareConfigFile);
PrepareFile(Application.ExeName, SaveDialog1.FileName, ConfigFile);
ConfigFile.Free;
end;
end;
procedure TForm1.LadenButtonClick(Sender: TObject);
begin
if OpenDialog1.Execute then
LoadConfigFile(OpenDialog1.FileName);
end;
Wenn du es jetz noch nicht hinbekommst, dann beherzige meinen Beitrag vor diesem hier!
Viel Glück!
________________________________
Edit:
Mir ist grad aufgefallen, dass es wahrscheinlich nicht klappen wird, da deine EXE ja ausgeführt wird. Du müsstest sie wahrscheinlich noch kopieren und dann wieder löschen.