0

In my WinForms app, if I set an icon for my app and an icon for my Form, the icon appears twice in my executable. Is it possible to avoid this?

(This question has been asked before, here, but the answers didn't seem to solve the problem. vanmelle's answer appears to extract only one icon (e.g., 16x16), Sunlight's answer extracts only the 32x32, and lc's answer doesn't solve the problem: there is still a duplicated icon in the executable.)

If it's not possible to accomplish this task, why is this? What is it about using the same icon for an executable and a Form that's so hard in WinForms?

Community
  • 1
  • 1
Tom
  • 817
  • 7
  • 14

1 Answers1

5

This is an inevitable consequence of running managed code on a completely unmanaged operating system. Windows Explorer only knows how to read unmanaged resources. You can see what they look like, use File + Open + File in Visual Studio and select your .exe. You'll typically see three resource groups listed there:

  • RT_MANIFEST, contains the manifest that tells Windows that your program is Vista aware
  • Version, contains the file version resource with values derived from your AssemblyInfo.cs file. You see its content when you use Properties + Details tab in Explorer. Note how the super important [AssemblyVersion] isn't visible in Vista and up
  • Icon, contains the icon you added.

This unmanaged resource data is separate from the managed resources you added. Managed resources are compiled into the assembly manifest. Unmanaged resources are stored in the .rsrc section of the image file. You can override the auto-generated version with the /win32res command line option. Which requires a .res file, a binary file that is generated from a .rc resource script by the rc.exe resource compiler. An age old Windows SDK tool.

This may change some day, the super-secret Midori project is a rumored to focus on a managed operating system. For now, we'll have to make do with the glue.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Sorry, I don't understand your reply. Are you saying that there is a way of avoiding the duplicated icon? If so, how do I achieve this using rc.exe? (Also, when I open my exe with File --> Open File, I don't see anything you mentioned above.) – Tom Sep 01 '11 at 17:46
  • "This is an inevitable consequence" means "no, can't be avoided". No idea why you don't see what I describe. File + Open + File in Visual Studio, this is supported at least since VS2005. Pick any file, like c:\windows\notepad.exe – Hans Passant Sep 01 '11 at 18:08