Embedded Binary Resources - how can I enumerate the image files embedded?

旧街凉风 提交于 2019-12-22 09:13:55

问题


I was following the instructions in this book, chapter on Resources etc. and what I can't quite figure out is, how do I replace this:

images.Add(new BitmapImage(new Uri(@"/Images/Deer.jpg", UriKind.Relative)));
images.Add(new BitmapImage(new Uri(@"/Images/Dogs.jpg", UriKind.Relative)));
images.Add(new BitmapImage(new Uri(@"/Images/Welcome.jpg", UriKind.Relative)));

by a loop that would simply run through all embedded image resources to avoid hard-coding the image names and paths. How would I go about discovering the names of the embedded files?

I did select the images I wanted to embed and specified:

Build Action=Resource and Copy to Output Directory=Do not copy

in the Properties window. The assembly has grown considerably in size and I am quite convinced the images have indeed been embedded.


回答1:


try something using GetManifestResourceNames, or look into the ResourceManager object.

Assembly assembly = Assembly.GetExecutingAssembly();
foreach (string s in assembly.GetManifestResourceNames())
{
    //match on filenames here. you can also extention matching to find images.
}


来源:https://stackoverflow.com/questions/3788504/embedded-binary-resources-how-can-i-enumerate-the-image-files-embedded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!