views:

64

answers:

4

Hi, the program crashes if there's no .ico file inside the same folder... I have:

1) Added the MyIcon.ico file in the Application section, also 'embed manifest with default settings' is checked.

2) Made the .ico file as Embedded Resource (Build Action) in the .ico file properties.

3) Added 'this.Icon = new Icon("plat.ico");' in the Public form.

So... why is the application not booting? What gives?

Thanks in advance.

+3  A: 

The constructor for Icon you are using tries to read "plat.ico" as a filename, not from embedded resources.

If you want to load the Icon from resources, you will need to explicitly get a Stream from the resource, then pass that into the Icon's constructor.

This will likely be something similar to:

// Add using System.Reflection; at the top of your file...

this.Icon = new Icon(
    Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace.plat.ico")
  );

Alternatively, you can use the constructor overload that pulls directly from a resource, by name, instead of a filename:

this.Icon = new Icon(this.GetType(), "plat.ico");
Reed Copsey
example of code, please?
Mark
@Mark: I was working on it ;) The string you pass in depends somewhat on where you put the icon file. Here's a link with more details: http://littletalk.wordpress.com/2010/02/18/how-to-load-an-icon-from-an-embedded-resource-in-c/
Reed Copsey
Doesn't work :/ I get this error: Additional information: Value of 'null' is not valid for 'stream'.
Mark
@Mark: I put another alternative, which is probably easier...
Reed Copsey
A: 

You need to set the icon file's "Copy to Output" to "Copy Always" or "Copy if Newer".

md5sum
A: 

According to your updated info, I think you shoud use the following:

ImageList myImageList = new ImageList();
myImageList.Images.Add((Image)new Bitmap(GetType(), "MyIcon.ico"));

You can add more icon files to the Image list if you wish & call them dynamically.

loxxy
Doesn't work, either. If I add that code to the public form, the .ico doesn't appear on the launched app.
Mark
A: 

You are invoking wrong constructor. Just use this:

this.Icon = new Icon(this.GetType(), "plat.ico");

tia