I am looking for a way to read an .ini
file to get the server host information from it. I got it loading static data, that part was easy, but I want to expand it so I can add more then one server in the .ini
, and then let my program add them in order.
[Data]
Server[1]=tcp/80.7.229.47/7799 // server one
Server[2]=udp/80.7.229.47/7780 // server two
Server[3]=tcp/80.7.229.47/7733 // server three
I would like to be able to add multiple servers, here 1 to 10 or whatever, then display the correct server information, the protocol and ip and port. I have done that via split code, but I don't know how to do multiple servers.
I was thinking maybe a for
loop to get the server number from .ini
file:
sData := IniFile.ReadString('Data', 'Server[' IntToStr(i) ']', '<none>');
Does anyone have any ideas how to do this?
CodePudding user response:
Drop the square brackets on the entry names:
[Data]
Server1=tcp/80.7.229.47/7799
Server2=udp/80.7.229.47/7780
Server3=tcp/80.7.229.47/7733
sData := IniFile.ReadString('Data', 'Server' IntToStr(i), '<none>');
To know how many servers are present, you could add a ServerCount
entry, eg:
[Data]
ServerCount=3
Server1=tcp/80.7.229.47/7799
Server2=udp/80.7.229.47/7780
Server3=tcp/80.7.229.47/7733
count := IniFile.ReadInteger('Data', 'ServerCount', 0);
for i := 1 to count do
begin
sData := IniFile.ReadString('Data', 'Server' IntToStr(i), '<none>');
...
end;
Or, you could use IniFile.ReadSection()
to get all of the entry names into a local TStringList
, then loop through that, eg:
names := TStringList.Create;
try
IniFile.ReadSection('Data', names);
for i := 0 to names.Count-1 do
begin
if StartsText('Server', names[i]) then
begin
sData := IniFile.ReadString('Data', names[i], '<none>');
...
end;
end;
finally
names.Free;
end;