简体   繁体   English

如何在列表中获取资源及其名称?

[英]How to get Resources and their names in a list?

This is my question's scenario:- 这是我的问题的方案:

  1. I have added a lot of images and other files(like .wav) in my Resources folder of project. 我在项目的Resources文件夹中添加了很多图像和其他文件(例如.wav)。 The image files go with names such as "woof1", "woof2"....."env1", "env2"......"alpha1", "alpha2"..... and so on. 图像文件带有“ woof1”,“ woof2” .....“ env1”,“ env2” ......“ alpha1”,“ alpha2” .....等名称。

  2. I want to access all image files starting with ONLY certain prefix (like "woof#" or "alpha#") and want to store these images in a List, and store the names of retrieved images in List. 我想访问仅以某些前缀开头的所有图像文件(例如“ woof#”或“ alpha#”),并希望将这些图像存储在列表中,并将检索到的图像的名称存储在列表中。

Please help me. 请帮我。 My project is to make a memory game..and the prefixes define different categories of images. 我的项目是制作一个记忆游戏。前缀定义了不同类别的图像。 I really need this to know. 我真的需要这个知道。

Are the resources added to the project file as a resource? 资源是否作为资源添加到项目文件中? If so, you can use Assembly.GetManifestResourceNames . 如果是这样,则可以使用Assembly.GetManifestResourceNames

Sample code from MSDN: Finding the Names of Resources in an Assembly : 来自MSDN的示例代码:在程序集中查找资源名称

System.Reflection.Assembly thisExe; 
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
string [] resources = thisExe.GetManifestResourceNames();
string list = "";

// Build the string of resources.
foreach (string resource in resources)
    list += resource + "\r\n";

Then after filtering the resource names to get which ones you need (eg using LINQ or creating a new List and populating it in a foreach loop, etc.), use Assembly.GetManifestResourceStream to actually get the resource. 然后,在过滤资源名称以获取所需的资源名称之后(例如,使用LINQ或创建新的List并在foreach循环中填充它,等等),请使用Assembly.GetManifestResourceStream实际获取资源。

If you haven't added the files to the project, I strongly recommend doing so as it means you'll have fewer files to distribute if you release it. 如果您尚未将文件添加到项目中,我强烈建议您这样做,因为这意味着如果发布文件,则可以分发的文件将更少。 See MSDN: resource files for how to do this. 有关如何执行此操作 ,请参阅MSDN:资源文件

See my answer in this post: 看到我在这篇文章中的答案:

https://stackoverflow.com/a/28558647/2416394 https://stackoverflow.com/a/28558647/2416394

Instead of fetching the resources by name you might use 不用按名称获取资源,您可以使用

var list = Assembly.GetExecutingAssembly().GetManifestResourceNames();

to obtain the resources and run a LINQ filter on them. 获取资源并对其运行LINQ过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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