WPF - check resource exists without structured exc

2019-03-14 02:09发布

问题:

Is there any way to check if a resource exists in an assembly without having to use exception handling? I'm currently loading images from several assemblies, and if they don't exist then I'm handling the IOException, which causes quite a bit of overhead.

回答1:

Would something like this work for you?

// Member Variable
string [] resourceNames;

// Function
Boolean ResourceExists(string resourceName)
{
    if (resourceNames == null)
    {
        resourceNames =  
            Assembly.GetExecutingAssembly().GetManifestResourceNames(); 
    }

    return resourceNames.Contains(resourceName);
}