Table of Contents

Class Resources

Namespace
Qt
Assembly
Qt.Bridge.CSharp.Api.dll

Provides access to app resources packaged with Qt Bridge for C#.

public static class Resources
Inheritance
Resources
Inherited Members

Remarks

Use this class when C# code needs to read a non-code file that ships with the app, such as an image, icon, font, text file, or JSON file. Qt Bridge packages these files into the app and gives each one a stable qrc:/ URL. QML can use the same URL directly, and C# can read the file content through this class.

The default URL shape is qrc:/assemblies/<AssemblyId>/<relative-path>. For example, a project named MyApp that includes icons/app.svg usually reads it as qrc:/assemblies/MyApp/icons/app.svg.

Add packaged files with <QtResource Include="..." />. Projects that already use .resx files can enable QtBridgeResourceLibrary to package file-reference entries the same way. Standard .NET ResourceManager access is separate and is only used when a resource is explicitly marked with QtResourceAccess.

All methods in this class validate that the URL starts with qrc:/.

Methods

Exists(string)

Returns true if a resource exists at the given qrc:/ URL.

public static bool Exists(string qrcUrl)

Parameters

qrcUrl string

The resource URL. Must start with qrc:/.

Returns

bool

Remarks

Use this method before optional resource reads:

if (Resources.Exists("qrc:/assemblies/MyApp/help/welcome.html")) {
    string html = Resources.ReadAllText("qrc:/assemblies/MyApp/help/welcome.html");
}

Exceptions

ArgumentException

qrcUrl does not start with qrc:/.

ReadAllBytes(string)

Reads the full content of a qrc:/ resource into a new byte array.

public static byte[] ReadAllBytes(string qrcUrl)

Parameters

qrcUrl string

The resource URL. Must start with qrc:/.

Returns

byte[]

Remarks

Use this method for binary resources such as images, fonts, and data files when the consuming C# API expects bytes or streams:

byte[] fontData = Resources.ReadAllBytes(
    "qrc:/assemblies/MyApp/fonts/Inter-Regular.ttf");
using var stream = new MemoryStream(fontData);

Exceptions

ArgumentException

qrcUrl does not start with qrc:/.

FileNotFoundException

The resource was not found in the packaged app resources.

IOException

The native read returned fewer bytes than expected.

ReadAllText(string, Encoding)

Reads the full content of a qrc:/ resource and decodes it as text.

public static string ReadAllText(string qrcUrl, Encoding encoding = null)

Parameters

qrcUrl string

The resource URL. Must start with qrc:/.

encoding Encoding

The text encoding to use. Defaults to UTF8 when null.

Returns

string

Remarks

The default encoding is UTF-8. Pass an explicit encoding when the resource uses a different text encoding:

string json = Resources.ReadAllText("qrc:/assemblies/MyApp/config/appsettings.json");
string legacyText = Resources.ReadAllText(
    "qrc:/assemblies/MyApp/text/legacy.txt",
    Encoding.GetEncoding("windows-1252"));

Exceptions

ArgumentException

qrcUrl does not start with qrc:/.

FileNotFoundException

The resource was not found in the packaged app resources.

Size(string)

Returns the byte length of the resource, or -1 if it does not exist.

public static long Size(string qrcUrl)

Parameters

qrcUrl string

The resource URL. Must start with qrc:/.

Returns

long

Remarks

A return value of -1 means the resource could not be opened:

long bytes = Resources.Size("qrc:/assemblies/MyApp/data/catalog.bin");
if (bytes >= 0) {
    Console.WriteLine($"Catalog size: {bytes} bytes");
}

Exceptions

ArgumentException

qrcUrl does not start with qrc:/.