Hello,
I have many XML files with "Build Action" set as "Resource". I can access them from code using the well known pack URI scheme, or just a relative URI (which is actually a valid pack URI, as stated by Microsoft in the pack URI msdn page, so everything is a pack uri :p), like this :
Uri uri1 = new Uri("pack://application:,,,/someFolder/myResource1.xml");
Uri uri2 = new Uri("someFolder/myResource2.xml");
Actually, I need to get a stream from each xml file. What I can do is the following :
Uri uri1 = new Uri("pack://application:,,,/someFolder/myResource1.xml");
var stream1 = App.GetResourceStream(uri1).Stream; // works fine
It works fine and I get my streams. Now here are my questions :
What if I do not know the names of my resources ? (only the path to "someFolder")
How can I list them ?
I cannot use Directory.GetFiles since resources are embedded into the assembly. A possible solution is to set the build action of my resources as "Embedded Resources" and then use the assembly.GetManifestResourceNames()
function, but I'm stubborn and do not want to change this build action :p
Thanks !
EDIT : I came to something like this :
/// <summary>
/// Returns a dictionary of the assembly resources (not embedded).
/// </summary>
/// <param name="filter">A regex filter for the resource paths.</param>
public static IDictionary<string, object> GetResources(string filter) {
var asm = Assembly.GetEntryAssembly();
string resName = asm.GetName().Name + ".g.resources";
Stream stream = asm.GetManifestResourceStream(resName);
ResourceReader reader = new ResourceReader(stream);
IDictionary<string, object> ret = new Dictionary<string, object>();
foreach (DictionaryEntry res in reader) {
string path = (string)res.Key;
if (Regex.IsMatch(path, filter))
ret.Add(path, res.Value);
}
return ret;
}