Enumerating .NET assembly resources at runtime

You can enumerate the AssemblyAssociatedContentFile attributes defined on the assembly :

var resourceUris = Assembly.GetEntryAssembly()
                   .GetCustomAttributes(typeof(AssemblyAssociatedContentFileAttribute), true)
                   .Cast<AssemblyAssociatedContentFileAttribute>()
                   .Select(attr => new Uri(attr.RelativeContentFilePath));

You can also check this page for a way to enumerate BAML resources.


UPDATE : actually the solution above works only for Content files. The method belows returns all resource names (including BAML resources, images, etc) :

    public static string[] GetResourceNames()
    {
        var asm = Assembly.GetEntryAssembly();
        string resName = asm.GetName().Name + ".g.resources";
        using (var stream = asm.GetManifestResourceStream(resName))
        using (var reader = new System.Resources.ResourceReader(stream))
        {
            return reader.Cast<DictionaryEntry>().Select(entry => (string)entry.Key).ToArray();
        }
    }

Leave a Comment