Search code examples
inno-setuppascalscript

Using Inno Setup LoadStringFromFile always gives "Type mismatch"


I am trying to read a simple text file, but whenever I use the function LoadStringFromFile it always gives me

Type mismatch.

The text file that I am trying to read from contains a file path to a folder in User Documents. Something like c:\users\username\document\foldername\.

I know that Inno Setup has a directory constant for User Document, but the users don't have rights to install. When running the installer with admin account the directory constant would give a different file path. This text file has the real file path that I want to use for the installation.

function getPath(Param: String) : String;
  var line: AnsiString;
begin
    Result := LoadStringFromFile(ExpandConstant('{commonpf64}\appfolder\sometextfile.txt'), line);
end;

Solution

  • The LoadStringFromFile function returns Boolean value indicating success of the read. The actual contents is returned in the S argument (or line in your code).

    Your function might look like:

    function getPath(Param: String): String;
    var
      Path: string;
      Line: AnsiString;
    begin
      Path := ExpandConstant('{commonpf64}\appfolder\sometextfile.txt');
      if LoadStringFromFile(Path, Line) then
      begin
        Result := Trim(Line); // Converts from AnsiString to string on the way
      end
        else
      begin
        RaiseException(Format('Error reading %s', [Path]));
      end;
    end;