10

The title sums it up pretty well. I know I can get the embedded resource names using:

        var assembly = System.Reflection.Assembly.GetExecutingAssembly();
        string[] files = assembly.GetManifestResourceNames();

But I'd like to be able to get the embedded resources from only a specific folder. Or at least be able to distinguish between embedded resources from different folders.

2 Answers 2

14

The resources are returned in the following format.

[Namespace].[Folder].[Filename]

Note that all folders in the path are separated by .'s. So if you had an embedded resource with the following configuration

myproject.csproj (Namespace = com.mycompany.myproject)
- Resources
  - Images
    - Icons
      - my_icon.ico

The resource name would be...

com.mycompany.myproject.Resources.Images.Icons.my_icon.ico

If you want to select all from a specific folder, you could use the following LINQ expression or modify it according to your needs.

string prefix = "your_namespace.your_folder."

var resourceNames = Assembly.GetExecutingAssembly()
    .GetManifestResourceNames()
    .Where(name => name.StartsWith(prefix));
5
  • This code will throw an error because it can't convert IEnumerable to string. Besides it doesn't account for more than one file under the same folder
    – Flavia
    Apr 11, 2013 at 19:10
  • Oops, pasted that code from another project and forgot to modify the type. I have corrected it now. Apr 11, 2013 at 19:14
  • 1
    This find all files in folder including subfolders! Further, if you had embedded file Res/my.special.file.txt and you searched for all files within Res/my/ it would be returned as well. Jan 14, 2017 at 18:53
  • It would be helpful to note that if your folder had a hyphen in the name, that gets 'converted' to an underscore for the resource name. I was bashing by head against my desk until I came here and used GetManifestResourceNames. Feb 19, 2018 at 15:17
  • This give me: cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'System.Reflection.Assembly' May 27, 2020 at 17:03
4

If you create a folder called Images, and place the file there, then the name of the resource becomes Assembly.Images.fileName.

So you can try something like:

var assembly = System.Reflection.Assembly.GetExecutingAssembly();

var names = assembly.GetManifestResourceNames().Where (n => n.Contains(".Images."));

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.