Jens Borrisholt
Jens Borrisholt

Reputation: 6402

Save assembly resource data to file

I have a c# program generating some WebPages. In my project I have added some JavaScript files to the project via the ResourceManager.

Now I want to get all the ResourceNames and save them to my Destination path.

I know this question have been asked a million times in here but I can not get it to work.

Here I try to list all my resources

foreach (var res in Assembly.GetExecutingAssembly().GetManifestResourceNames())
{
    ....
}

But I do not get the Resource names in res but "WindowsFormsApplication3.Form1.resources" the first time in the loop and "WindowsFormsApplication3.Properties.Resources.resources" second time and "WindowsFormsApplication3.Properties.Resources.Designer.cs" third time

What am I doing wrong?

Upvotes: 0

Views: 121

Answers (1)

Faris Zacina
Faris Zacina

Reputation: 14274

You are just getting the names of the manifest resources, which is not the same thing as Resource file (resx) resources.

To get the resource file resources from a manifest resource file name like "WindowsFormsApplication3.Properties.Resources.resources" you would have to do:

foreach (var manifestResourceName in Assembly.GetExecutingAssembly().GetManifestResourceNames())
{
    using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(manifestResourceName))
    {
        if (stream != null)
        {
            using (var rr = new ResourceReader(stream))
            {
                foreach (DictionaryEntry resource in rr)
                {
                    var name = resource.Key.ToString();

                    string resourceType;
                    byte[] dataBytes;

                    rr.GetResourceData(name, out resourceType, out dataBytes);
                }
            }
        }
    }
}

And then you can save the bytes wherever you want.

Upvotes: 1

Related Questions