Using a resource file for icons

J

J.Marsch

Hello all:

I am trying to build a resource dll that will house icons, and I'm afraid
that I'm not having much luck reading it. Can you lend hand (or just point
me to a good primer).

Here's what I've done so far: (VS.NET 2003/C#)

1. Created an empty project for the resource dll. Output type is class
library.
2. Added a bunch of icon files to the project (add existing item).
3. Set the build action of each icon file to "embedded resource".
4. So, this project is of type class library, has no references, and only
contains .ico files, no .cs files.

Next, in a separate test exe, I run the code below to try to simulate
reading the resources. I get an ArgumentException in the constructor of the
ResourceReader: "Stream is not a valid resource file". (the code is below)

I think that either I am not building the resource dll correctly, or I am
not using the right resource class to try to read it. Any hints?


private void ReadResources(string resourceFileName) // resourceFileName ==
"myResources.dll"
{
System.Resources.ResourceReader reader = new
System.Resources.ResourceReader(resourceFileName);
try
{
System.Collections.IDictionaryEnumerator enumerator =
reader.GetEnumerator();
while(enumerator.MoveNext())
System.Diagnostics.Debug.WriteLine(enumerator.Key);
}
finally
{
reader.Close();
}

}
 
D

Dustin Aleksiuk

J.Marsch said:
Hello all:

I am trying to build a resource dll that will house icons, and I'm afraid
that I'm not having much luck reading it. Can you lend hand (or just point
me to a good primer).

I was just stuck on this too. I solved it by using a .resources file
which I linked in to the application the same way you mention in your
description (by setting that 'embeddable' flag on).

You can call it with the ResourceManager class. The string you pass into
the constructor is the name of the resources file without the suffix,
and you have to make sure that if you have an application namespace you
prepend it to the name.

So if my resources file was "myapplication.resources" and my application
namespace set in Visual Studio was "mynamespace" I would pass in
"mynamespace.myapplication" into the constructor. You can then just call
..GetResourceSet on it.

I hope that helps a little.

Regards,
Dustin Aleksiuk
 
D

Dustin Aleksiuk

J.Marsch said:
Hello all:

I am trying to build a resource dll that will house icons, and I'm afraid
that I'm not having much luck reading it. Can you lend hand (or just point
me to a good primer).

Here's what I've done so far: (VS.NET 2003/C#)


One more thing: you can use ILDasm.exe to check your .DLL and see if the
resources file got linked in properly. It will also tell you if there is
a namespace you need in order to find it.

Hope I'm not out to lunch with this, but it worked for me yesterday.

Regards,
Dustin Aleksiuk
 
J

J.Marsch

Thanks for your reply, Dustin. I think that I found an additional way to do
it (would love to hear from anyone whether this is the best way):

To get the list of resources (note: this ugly test code is using a Listbox
to display the resource names):
// this code works with a resource dll.
private void ReadResourcesFromManifest(string resourceFileName)
{
this.ResourcesListBox.Items.Clear();
this.TargetAssembly =
System.Reflection.Assembly.LoadFile(resourceFileName);
this.ResourcesListBox.Items.AddRange(this.TargetAssembly.GetManifestResourceNames());
}

Now, this code can load a resource into a picture box (and again, sorry for
the ugly code -- this is just a test project)
private void ResourcesListBox_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if(this.ResourcesListBox.SelectedIndex >= 0 && this.TargetAssembly !=
null)
{
System.Drawing.Image image =
System.Drawing.Image.FromStream(this.TargetAssembly.GetManifestResourceStream((string)this.ResourcesListBox.SelectedItem));
this.pictureBox1.Image = image;
}
}

Of course, this code assumes that the incoming resource is a bitmap. I
would really like a good way to verify the type of the incoming stream. It
seems like a possible attack vector if someone slips me a non-bitmap
resource.

Also, I think that loading the assembly in this way means that it cannot be
unloaded. I wonder if there is a better way load it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top