1

I'm using Visual Studio 2010 with a C# WPF app and I've added some 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?

Jack
  • 3,769
  • 6
  • 24
  • 32

1 Answers1

1

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.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400