Checking if resource file exists

If you do not want to have it bundled to the output folder additionally - you do not have to do anything. It is build into your exe, not need to check. Would always be true.


Okay, I understand because you dynamically build the name of your embedded resource you want to check it.

See here: WPF - check resource exists without structured exception handling

They basically check against Assembly.GetExecutingAssembly().GetManifestResourceNames()

You can use that as a starting point. But note that the resource name is not images/myimage.png but constructed from your namespace like YourApp.images.myimage.png. You might like to take a look at the contents of the built resourceNames array from that answer.


Have you set the "Copy to the Output" property to "Always"? And make sure that you use the correct path. The path of your executing assembly can be detected by using following code:

private string GetExecutingAssemblyPath()
{
    string codeBase = Assembly.GetExecutingAssembly().CodeBase;
    UriBuilder uri = new UriBuilder(codeBase);
    string path = Uri.UnescapeDataString(uri.Path);
    return Path.GetDirectoryName(path);
}

Cheers.

Tags:

C#

Wpf