Home Delphi XE6: How to load resource into TMemIniFile?
Reply: 1

Delphi XE6: How to load resource into TMemIniFile?

Vlastimil
1#
Vlastimil Published in 2017-09-12 07:33:31Z

I've been running Delphi XE6 and am trying to put read-only INI files into the EXE file.

That I have already solved. Also loading the files is working fine into TMemo.

I have defined the following function loading the pure TXT INI file from resource into TMemo:

procedure LoadTXTfromResource(const ResourceName: string; OutMemo: TMemo);

var
  ResourceStream: TResourceStream;

begin
  ResourceStream := TResourceStream.Create(HInstance, ResourceName, RT_RCDATA);
  try
    OutMemo.Lines.LoadFromStream(ResourceStream);
  finally
    ResourceStream.Free;
  end;
end;

It is working flawlessly.

Now, what I need, is to re-write the procedure to load the aforementioned text file into TMemIniFile.


I've been trying various things, but I can't seem to be able to do that. Any hints?

David Heffernan
2#
David Heffernan Reply to 2017-09-12 07:49:09Z

You need to load the resource stream into a string list, and then transfer to the TMemIniFile.

procedure LoadINIfromResource(const ResourceName: string; IniFile: TMemIniFile);
var
  ResourceStream: TResourceStream;
  Text: TStringList;
begin
  ResourceStream := TResourceStream.Create(HInstance, ResourceName, RT_RCDATA);
  try
    Text := TStringList.Create;
    try
      Text.LoadFromStream(ResourceStream);
      IniFile.SetStrings(Text);
    finally
      Text.Free;
    end;
  finally
    ResourceStream.Free;
  end;
end;

Note that because your TMemIniFile is not file based, you must pass an empty string as the file name when instantiating it:

IniFile := TMemIniFile.Create('');
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.321965 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO