function RetrieveApplicationPath(NormalPath:
string):
String;
var
size: integer;
dirTmp:
string;
tmpDelimiter:
string;
begin
tmpDelimiter := IncludeTrailingPathDelimiter('
');
size := length(NormalPath) - 1;
// -1 to remove the / at end
dirTmp := Copy(NormalPath, 1, size);
while (dirTmp[size] <> tmpDelimiter)
and (size > 2)
do
dec(size);
Result := Copy(NormalPath, 1, size);
end;
procedure TFMain.WriteConfig;
var
textFileTmp: TextFile;
strTmp:
string;
error: boolean;
begin
error := false;
// Try to open the file for writing and write some lines
AssignFile(textFileTmp, ConfigFileName);
try
ReWrite(textFileTmp);
except
error := true;
end;
if error
then
exit;
Writeln(textFileTmp, setupIP);
Writeln(textFileTmp, setupPort);
CloseFile(textFileTmp);
end;
procedure TFMain.ReadConfig;
var
textFileTmp: TextFile;
strTmp:
string;
begin
// Reopen the file for reading only and display content
if fileExists(ConfigFileName)
then
begin
AssignFile(textFileTmp, ConfigFileName);
Reset(textFileTmp);
ReadLn(textFileTmp, setupIP);
ReadLn(textFileTmp, setupPort);
CloseFile(textFileTmp);
end
else
begin
setupIP := '
192.168.0.1';
setupPort := '
81';
end;
end;
procedure TFMain.FormCreate(Sender: TObject);
begin
ApplicationPathImages := ExtractFilePath(paramstr(0));
ApplicationPath := RetrieveApplicationPath(ApplicationPathImages);
DocumentDirDelphi := ApplicationPath + '
Documents/';
ConfigFileName := DocumentDirDelphi + '
config.txt';
ReadConfig;