Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through Embedded Resources and copy to local path

I have a simple WinForms application, but it has some Embedded Resources (in a subfolder under "Resources") that I would like to copy out to a folder on the computer. Currently, I have the latter working (with a explicit method naming the Embedded Resource and where it should go):

string path = @"C:\Users\derek.antrican\";

using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream("WINFORMSAPP.Resources.SUBFOLDER.FILE.txt"))
using (Stream output = File.Create(path + "FILE.txt"))
{
    input.CopyTo(output);
}

But I'm still trying to figure out how to get the former working: looping through all the resources in the "WINFORMSAPP.Resources.SUBFOLDER" folder and moving them. I've done quite a bit of Googling, but I'm still not sure how to get a list of each Embedded Resource in this subfolder.

Any help would be GREATLY appreciated!

like image 951
derekantrican Avatar asked Mar 13 '23 09:03

derekantrican


1 Answers

Start by getting all resources embedded in your assembly:

Assembly.GetExecutingAssembly().GetManifestResourceNames()

You can check these names against the name of your desired subfolder to see if they are inside or outside it with a simple call to StartsWith.

Now loop through the names, and get the corresponding resource stream:

const string subfolder = "WINFORMSAPP.Resources.SUBFOLDER.";
var assembly = Assembly.GetExecutingAssembly();
foreach (var name in assembly.GetManifestResourceNames()) {
    // Skip names outside of your desired subfolder
    if (!name.StartsWith(subfolder)) {
        continue;
    }
    using (Stream input = assembly.GetManifestResourceStream(name))
    using (Stream output = File.Create(path + name.Substring(subfolder.Length))) {
        input.CopyTo(output);
    }
}
like image 116
Sergey Kalinichenko Avatar answered Mar 27 '23 04:03

Sergey Kalinichenko