简体   繁体   中英

C# Get embedded resources from specific folder or distinguish between embedded resources in different folders

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.

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));

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."));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM