I have many XML files in which "Build Action" is set to "Resource". I can access them from the code using the well-known package URI scheme or only the relative URI (which is actually a valid package URI, as indicated by Microsoft on the package msdn UID page, so this is all a uri: p package), for example 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. I can do the following:
Uri uri1 = new Uri("pack://application:,,,/someFolder/myResource1.xml"); var stream1 = App.GetResourceStream(uri1).Stream;
It works great and I get my threads. 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 because resources are built into the assembly. A possible solution is to set the assembly action of my resources to "Embedded Resources" and then use the assembly.GetManifestResourceNames() function, but I am stubborn and don't want to change this assembly action: p
Thanks!
EDIT: I came up with 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; }