Accessing Embedded Resources on .NET
On .NET, Embedded Resources can be accessed by obtaining a Stream via the Assembly.GetManifestResourceStream
.NET API, typically on the assembly that contains the current code, or on Assembly.GetEntryAssembly
for the main executable:
var lAssembly := typeOf(self).Assembly;
var lResourceStream := lAssembly.GetManifestResourceStream("MyText.txt");
if assigned(lResourceStream) then begin
using lReader := new System.IO.StreamReader(lResourceStream) do begin
var lText := lReader.ReadToEnd();
...
end;
end;
var assembly = typeof(this).Assembly;
var resourceStream = assembly.GetManifestResourceStream("MyText.txt");
if (resourceStream != null)
{
using (var reader = new System.IO.StreamReader(resourceStream))
{
var text = reader.ReadToEnd();
...
}
}
let assembly = dynamicType(this).Assembly
if let resourceStream = assembly.GetManifestResourceStream("MyText.txt") {
__using let reader = new System.IO.StreamReader(resourceStream) {
let text = reader.ReadToEnd()
...
}
}
var assembly = typeOf(this).Assembly;
var resourceStream = assembly.GetManifestResourceStream("MyText.txt");
if (resourceStream != null)
{
using (var reader = new System.IO.StreamReader(resourceStream))
{
var text = reader.ReadToEnd();
...
}
}
The resource name is typically composed of the project's RootNamespace and the path plus filename of the resource relative to the project. If you embed a text file named "MyText.txt" that is placed in the root of a project with default namespace "MyCompany.MyProduct", the resource name would be "MyCompany.MyProduct.MyText.txt".
A list of all resources in an assembly can be obtained using Assembly.GetManifestResourceNames
.