Click here to Skip to main content
Email Password   helpLost your password?

Introduction

I have noticed in various (and lots of) places scattered all across the Internet, requests on how to embed multiple icons into a .NET executable, not embedded in the .NET resources (resx files) but accessible by the Windows Explorer and the like.

After chance comment on a page with nothing to do with the subject at hand, I realised that this capability is built into Visual Studio 2005. This article outlines the procedure required to compile multiple icons into an executable without having to resort to the command-line.

The Process

Step 1: The Resource File

For reasons that shall become apparent later, Visual Studio requires that the icons be compiled into a resource file. It is up to you how you generate this, but I shall use the designers with VS2005.

  1. Create a new "Native Resource Template" from the File | New dialog box.
  2. New Native Resource Template

  3. Right click anywhere in the designer, and select "Add Resource..." to reach the following dialog box:
  4. Import Resources Dialog Box

  5. You can, at this point, import any resources you wish to embed, but we will import some icons, so click "Import..." and select the icon files.
  6. Import File Dialog Box

    If you wish, you can change the ID associated with the resources in the Properties window.

  7. Now, we must save the resource template as a 32-bit Resource File. This is done via File | Save As..., making sure that "32-bit Resource File (*.res)" is selected in the "Save as type" box. Save it somewhere logical, e.g., in your project's directory.
  8. Saving the Resource File

Step 2: Compiling into the Executable

Visual Studio 2005 provides two ways of providing icons for .NET executables. One is via the Application Icon property in the Property pages. The second is via a resource file, and it is this method that we utilise.

  1. Open up the Application tab of the Properties page of your project.
  2. In the "Resources" group box, change the selection to point to Resource File, and browse for the resource file you just saved.
  3. Add Win32 Resource

Step 3: Compiling

Once the previous step has been completed, Visual Studio will now embed the Win32 resource into the executable when it compiles. To see the changes, simply recompile the application.

Multiple Icon Executable

You can see by the above screenshot that multiple icons have been embedded (natively) into the executable. Where this would be useful, for example, is if your application registers a file type, the files can have a different icon associated with them rather than the same as the executable. Or you won't have to distribute an icon file with your program, it can simply be embedded.

Summary

Hopefully, that has explained clearly enough how to accomplish the task of embedding multiple icons into a .NET executable without resorting to the command-line. If you have any suggestions for improvements / comments, don't hesitate to post them, I've probably made quite a few spelling mistakes along the way as well (no Intellisense).

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralWarning: This breaks File Properties and msi Upgrades
stevenmcohn
4:15 2 Mar '10  
Very good article and I appreciate your investigation.   I started using this in one of my own projects to embed a second icon for my program's Start menu Uninstall item (my program knows how to uninstall itself given a /u parameter.)

However, by using a .res file and changing the Properties "Resources" to use a Resource file, you've now overridden the information specified in the AssemblyInfo.cs file.   Most of the [assembly: ...] attribute directives are now ignored.  

This means that attributes such as AssemblyCompany and AssemblyCopyright are no longer visible in the File Properties dialog.   Worse, this breaks the upgrade feature of Deployment project .msi setups files since we no longer have a link to AssemblyVersion.   By switching the project Properties Resources back to a simple icon, the compiler once again considered the AssemblyInfo attributes and the .msi upgrade started working.

So while I'm sure this is useful for many people, many others should be aware of these caveates.

Kind Regards

P.S. For my multi-icon dilema, I ended up deploying a second icon in the install directory that I could bind to my Uninstall Start menu item.   It means there's an extra .ico file out there but OK.
GeneralThere is no resources box
rocketman221
15:08 15 Dec '09  
There is no resources box on the application tab. And putting it in the resources tab doesn't work.
QuestionGreat... but how to extract them?
J. Elliott
12:11 5 May '09  
This is a fabulous article, but now that I have a .res file with four icons in it, how do I get any of them other than the first to show up in Windows Explorer?

Specifically I've got one big project that I build four different ways with four Configurations. The code is nearly identical for all four builds except where I use #if...#endif to check for one of four conditional compilation symbols assigned to each configuration.

Now instead of changing the Resources / Icon and Manifest / Icon setting between each build, I have just one entry in the Resources / Resource file field, which indicates my Win32Resource I built thanks to your article. However, I want to extract a certain one of the four icons inside it for each of my 4 configurations.

I think this article stopped short of showing how to do that. Thanks!

Jeff Elliott
Pres & Owner, C Guru, Inc.

AnswerRe: Great... but how to extract them?
Ed.Poore
23:39 5 May '09  
From what I remember the first icon in the list will be the one which is displayed in Windows Explorer (i.e. the one with the lowest index). The others should be visible if you're using explorer to create a shortcut to the program then you can specify which icon you want to use in the shortcut properties. Usually gives the file path followed by a , and then the icon index.
J. Elliott wrote:
I think this article stopped short of showing how to do that. Thanks!
Would be a good expansion, e.g. different icons for debug builds etc. I'm not sure how this could be accomplished, perhaps by a custom build action or pre/post build tasks.

One method I can think of off the top of my head would be:
  1. Create a folder with all the resource files with one named for each configuration
  2. In the pre-build event write a small script which copies Resources\$(ConfigurationName).res to a default location
  3. Link the default location as the resource file for the project



I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

GeneralRe: Great... but how to extract them?
J. Elliott
11:04 6 May '09  
Good ideas. I'll try them shortly. (My first post here ever, BTW!)

Jeff Elliott
Pres & Owner, C Guru, Inc.

GeneralGood Tutorial
ctw313
0:57 8 Nov '08  
Finally an Icon Embedding tutorial that's TO THE POINT! Big Grin
GeneralRe: Good Tutorial
Ed.Poore
11:42 8 Nov '08  
Thanks, I prefer short and to the point stuff. My pet hate is screen casts, mainly because I'm a fast reader and can read much faster than most people can speak...


Answerfor Visual Studio 2008
NevzatAdagide
13:58 18 Oct '08  
Direct open exe in ide.

http://download.efe-bilgisayar.com.tr/anlatim/snap1.jpg[^]

http://download.efe-bilgisayar.com.tr/anlatim/snap2.jpg[^]

http://download.efe-bilgisayar.com.tr/anlatim/snap3.jpg[^]

http://download.efe-bilgisayar.com.tr/anlatim/snap4.jpg[^]

http://download.efe-bilgisayar.com.tr/anlatim/snap5.jpg[^]

http://download.efe-bilgisayar.com.tr/anlatim/snap6.jpg[^]
QuestionRe: for Visual Studio 2008
Ed.Poore
21:22 18 Oct '08  
So you prefer doing 6 additional steps for every rebuild of the project as opposed to a couple to start with?


I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

GeneralImport of .res file not available in VS 2008
t_anjan
9:23 11 Feb '08  
In the Resources tab of the Project Properties page, there is no option to import .res files. The only workaround I could find was to use a /win32resource compiler switch. More details in this thread[^].
GeneralRe: Import of .res file not available in VS 2008
Ed.Poore
9:50 11 Feb '08  
Take a look at this[^] screenshot.

Sorry I don't have VS2005 around so can't point it out.


GeneralDoes not appear to work with 32-bit icons.
Ineffable
10:43 1 Aug '07  
Is there anyway to embed 32-bit icons?
GeneralRe: Does not appear to work with 32-bit icons.
Ed.Poore
14:23 1 Aug '07  
I think that that may be a problem with VS2005, if you try and edit 32 bit icons it complains about it.  Unfortunately I don't have the time at the moment to investigate.

Do you get an error message or anything in particular?



GeneralGood one!
Vasudevan Deepak Kumar
0:26 13 Jul '07  
Good one and informative too.

Vasudevan Deepak Kumar
Personal Homepage Tech Gossips

QuestionCan't add Win32 resource into VB.NET project [modified]
ProJee
23:34 12 Jul '07  
Step 2.2:

There's no "Resources" group box in the properties of VB.NET project (and I've never seen it there before)... Cry

modified on Tuesday, May 5, 2009 4:10 AM

AnswerRe: Can't add Win32 resource into VB.NET project
Chris Kolkman
4:03 13 Jul '07  
It's actually a tab along the left side:

Application*
Compile
Debug
References
Resources Settings
Signing
Security
Publish
GeneralInteresting...
AnandChavali
20:26 12 Jul '07  
Nice one dude..

Thanks and Regards,
Anand.

GeneralRe: Interesting...
Ed.Poore
1:51 13 Jul '07  
Ah well, C# it is then Poke tongue .  I hadn't checked but the win32 resource thing is a feature of the C# compiler.  I don't know if there's one for VB.NET.  Normally if you were doing this from the command line you'd do
csc.exe /win32res:application.res



GeneralRe: Interesting...
Ed.Poore
4:23 13 Jul '07  
Sorry that wasn't meant to be posted under your message but the one above.



GeneralNice One
Paul Conrad
19:24 12 Jul '07  

Ed,

Very nice, quick article. One worthy of going into my CP blog for good reading Big Grin

Paul



"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak


Last Updated 12 Jul 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010