Loop through Visual Studio resources
I'm using Visual Studio 2010 with a C# WPF app and I've added s开发者_如何学JAVAome images into a subfolder called assets. Is there any way I can loop through all the images that i've added to the folder using pack URIs or something similar?
The following method gets all the file names in a resource-folder:
public static string[] GetResourcesUnder(string folder)
{
folder = folder.ToLower() + "/";
var assembly = Assembly.GetCallingAssembly();
var resourcesName = assembly.GetName().Name + ".g.resources";
var stream = assembly.GetManifestResourceStream(resourcesName);
var resourceReader = new ResourceReader(stream);
var resources =
from p in resourceReader.OfType<DictionaryEntry>()
let theme = (string)p.Key
where theme.StartsWith(folder)
select theme.Substring(folder.Length);
return resources.ToArray();
}
Just need to put the head back on when you use them:
var files = GetResourcesUnder("Images");
foreach (var file in files)
{
string uriPath = "pack://application:,,,/Images/" + file;
//...
}
I did not write this method, it's from another question here on SO, i'll try to find it...
Edit: It's from here.
精彩评论