Search code examples
delphidelphi-xe8

Add ini file as resource file and read from it


I would like to add an ini file to my delphi project as a resource file.

I know where you go to add a file as a resource file:

Project > Resources and Images > Add

But once thats done what else do I need to do to be able to read from the file? I haven't used resource files before.

Is there any documentation on the process?

Thanks,


Solution

  • To add the files in the executable, be sure to add a .rcfile (not the .ini files directly), for example inis.rc (so in the source file dpr you'll have {$R 'inis.res' 'inis.rc'}) and write in this file a list like this:

    this 100 "this.ini"
    that 100 "that.ini"
    

    If you've stored the ini files in a (relative) directory, be sure to double the backslashes since this in C-syntax. (The 100here is the resource type, there's no number assigned to ini-files specifically so we'll use an unassigned number. The best next thing is 23 which is assigned to RT_HTML, see below)

    If you're not using groups (and lines with just [GroupName]), I'd suggest you use plain TStringList objects and their Values property. To load them with the data, use something like this:

    var
      sl:TStringList;
      r:TResourceStream;
    begin
      sl:=TStringList.Create;
      try
        r:=TResourceStream.Create(HInstance,'this',MAKEINTRESOURCE(100));
        try
          sl.LoadFromStream(r);
        finally
          r.Free;
        end;
    
        //sl.Values['Setting']
    
      finally
        sl.Free;
      end;
    end;