How to change the default Icon of a win Form and including it in the .exe file?

Oct 6 2009 10:10 AM
Dear All,
I am new programming in c#, the first programs I am developing are not too big and I am developing them by hand, it means I am not using the VS wizzards. I compile the applications (console or winapp) from the command line. My problem is that I don't know how to change the default Icon of a Form and including it in the exe file. I mean I know that I have to use :

this.Icon = new Icon("myicon.ico");

to assign the Icon. The application works if I have the file myicon.ico in the same directory as the application but if I try to execute the application in another directory it crashes because it doesn't find the icon file, it produces a portability problem, I have already try with other code like:

Icon icon1 = (System.Drawing.Icon)this.FormResources.GetObject("myicon.ico");

or

Stream strm = (Stream) Assembly.GetExecutingAssembly().GetManifestResourceStream("myicon.ico");
            this.Icon = new Icon(strm); <-- this didn't work I found the example in internet but I don't know if the type stream is correct to define an Icon.

and always I get the same problem or a worse one.

what ever, now I am lost in a cycle and I ask for your help may be the solution is not so complicated, but I would appreciate the replies and if someone help me to solve the problem.

thank you very much in advance

desperate Oriana




Answers (3)