Resource Files Support
Go Up to Resources and Images
Resource files are only supported for desktop platforms. In mobile platforms, the OS does not accept loading from resource files. To load files to a mobile device use a different approach, by loading the files externally.
Desktop Platforms
This example shows how to load the text from a txt file using a resource file.
To build and test this example:
- Create a Multi-Device Application.
- Add the following controls on the form:
- Add the txt file to the project's folder.
- Go to Project > Resources and Images... and add the txt file.
- Build the project to generate the .rc file that contains the reference to the txt file.
- Open the <project_name>_<resources>.rc file where you can see the name of the resource, the type, and the name of the file.
Resource_1 RCDATA "<file_name>.txt"
- Note: You can see all this information on the Resources and Images... window.
C++Builder
void __fastcall TForm1::Button1Click(TObject *Sender) {
TResourceStream *Stream = new TResourceStream((int)HInstance, "<Resource identifier>", RT_RCDATA); //RT_RCDATA is the Resource Type.
__try {
TStringList *List = new TStringList;
__try {
List->LoadFromStream(Stream);
Label1->Text = List->Text;
}
__finally {
List->DisposeOf();
}
}
__finally {
Stream->DisposeOf();
}
}
Delphi
procedure TForm1.Button1Click(Sender: TObject);
var
List: TStringList;
Stream: TResourceStream;
begin
Stream := TResourceStream.Create(HInstance, '<Resource identifier>', RT_RCDATA);
try
List := TStringList.Create;
try
List.LoadFromStream(Stream);
Label1.Text := List.Text;
finally
List.Free;
end;
finally
Stream.Free;
end;
end;
You can run this example on mobile platforms, but you will get an exception when trying to load the resources: class EResNotFound with message "Resource <resource_name> not found" .
Mobile Platforms
This example demonstrates how to load the text from a text file without using a resource file.
To build and test this example, create a Multi-Device Application, and add the following controls on the form:
- Copy the txt file to your project's folder.
- Use the Deployment Manager to place the file in the
assets\internal
(internal) orassets
(external) folder during deployment. - Include this unit to your project: System.IOUtils, to use GetDocumentsPath.
In C++Builder
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TStringList *List=new TStringList;
__try {
List->LoadFromFile(System::Ioutils::TPath::GetDocumentsPath + PathDelim + "example.txt"); // #include <System.IOUtils.hpp>
Label1->Text = List->Text;
}
__finally {
List->DisposeOf();
}
In Delphi
procedure TForm6.Button1Click(Sender: TObject);
var
List: TStringList;
begin
List := TStringList.Create;
try
List.LoadFromFile(TPath.GetDocumentsPath + PathDelim + 'example.txt');
Label1.Text := List.Text;
finally
List.Free;
end;
end;