• 0

[C#] Icon resource is duplicate in the built executable


Question

Hi,

When my application doesn't have any icon resources, all other resources and code produce a 121kb exe file. Then, I manually add the application icon resource to the "Resources.resx" file under the "Icons" category. After that, I compile the application again and since my icon has 66kb, the exe becomes 187kb. Then I open my application project settings and set the application icon by selecting it from the dropdown. After compiling the application again, the exe is now 253kb (187 + 66).

How is that possible? I'm using the same resource, why does the exe size increases like this?

Anyway to workaround the problem?

17 answers to this question

Recommended Posts

  • 0

^ It's actually a good idea. These days, programmers tend to not care about memory usage; after all, 66kb isn't much memory at all. But neither is 1MB or even 10MB. But wasted memory is wasted memory. The smaller the size, the better. :)

  • 0

i guess wad u could do is either just by choosing the application icon from the browse button.. which will not end up in the resources or by adding it into the resources but not compiling the resources as a embedded content so it will not get embedded twice.. u can do that by clicking on resources.resx in the solution explorer and changing the build action to none..

either way it doesnt hurt does it >.>

  • 0
I'm not sure what you mean...

first way: (not adding icon to resources)

under project properties > application (the tab where u select your icon) > browse to the icon in your drive

second way: (add icon to resources but dont embed the resources)

add the icon to the project resources then click on Resources.resx under the solution explorer and change the build action property from Embedded Resource to None

either way so the icon will not get embedded twice =.= zzz

  • 0

I can't do either of that...

I want my icon to be the icon for the .exe file and for the main application window. For the application window, I have to add it to the resources file (or to the form resources, but it will be the same thing). And since I need to add the icon to the resources file to use it as the form icon, why can't I just select "use the same icon" for the .exe?

  • 0

Looks that way mate, I can't really provide a real solution. I know how annoying things like this can be, especially if you are a perfectionist like me. And by the sound of most of your posts, you are.

Will keep an eye out for solutions, but at the moment, I cant find any.

  • 0

Hi guys,

I was just wondering if any of you had figured out a solution to this problem? I am having the same issue in VS2008 c#. I add the icon on the properties page which causes it to be embedded but I cannot then get access to it from the:

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("resource_file_name");

It does not appear to exist in the assembly, I use the GetManifestNames function to read all the resources and it is not listed.

To make it work I must also add the icon file in solution explorer and set it as an embedded resource, then I am able to reference it in this fashion. Each form can then reference this resource without it being embedded even more times which is what happens if you just choose the icon file from the form properties menu (rubbish).

Oh and also with my method of loading the resource file by hand at form startup it does not get displayed in the design window, also rubbish.

So I have limited it to a single duplicate for my application but it would be nice to cut this down to one, surely it is possible.

Can the .exe be specified from the code/by hand without using the properties page? Then you could use a single .ico resource.

cheers

ed

  • 0

I think you'd need to access the resource in a different way. The actual program icon would be stored within a specific .rsrc section of the PE format of the file. An executable .NET assembly is a normal Windows PE file with .NET headers. The main icon resource for the program would be stored according to the PE file format, so would not be accessible through the ManifestResourceStream method.

  • 0

i have skim read this thread, so excuse me if i have the wrong end of the stick..

but there is a bug(?) in visual studio that does not show the icon you select in your project properties screen on the windows forms during debug mode..

have you tried building installer packages for your applications and installing them to your local system?

the icons *should* appear in your app window at the top left

  • 0

hey,

I haven't tried that. I'm hoping my application will be a single executable and not need too much installing but it is a good suggestion. Maybe we just have to accept two copies of the icon file, I suppose it's not too bad, it would be nice to see a fix tho.

ed

  • 0

I came across this thread through Google some time ago when writing an application because I had the same problem as the OP. I just lived with the fact that I would have to have the icon in the executable twice. I had the same problem again, today, and a light bulb went off. This still isn't as clean as it should be, but you can do it like this:

In your application's properties (Project -> Properties), set the icon to the file you want it to use. Leave the icon for the form as the default. In the form's constructor (under InitializeComponent()) or in the form's Load event, add this:

this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);

This get's the application's icon that you set in the properties (and the one that is seen by Explorer, etc) and sets it as your form's icon at run-time. Hope this helps anyone Googling in the future.

Edit: It's worth noting, however, that this doesn't seem to work when running the application from anything other than the local machine (i.e. from a Windows share).

Edited by todd
  • 0
Edit: It's worth noting, however, that this doesn't seem to work when running the application from anything other than the local machine (i.e. from a Windows share).

It'd be more efficient to PInvoke the LoadIcon API function, and this will prevent you from being snagged by the file location security checks. This will get you an HICON that you can then use Icon.FromHandle on.

Google PInvoke+LoadIcon

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Not even an OLED display on the laptops. Also it seems that the laptop design isn't the same as the Surface Ultra model. Looks like bargain bin at high prices.
    • VirtualBox 7.2.10 by Razvan Serea VirtualBox is a powerful x86 and AMD64/Intel64 virtualization product for enterprise as well as home use. Targeted at server, desktop and embedded use, it is now the only professional-quality virtualization solution that is also Open Source Software. Presently, VirtualBox runs on Windows, Linux, macOS, and Solaris hosts and supports a large number of guest operating systems including but not limited to Windows (NT 4.0, 2000, XP, Server 2003, Vista, 7, 8, Windows 10 and Windows 11), DOS/Windows 3.x, Linux (2.4, 2.6, 3.x, 4.x, 5.x and 6.x), Solaris and OpenSolaris, OS/2, OpenBSD, NetBSD and FreeBSD. Some of the features of VirtualBox are: Modularity. VirtualBox has an extremely modular design with well-defined internal programming interfaces and a client/server design. This makes it easy to control it from several interfaces at once: for example, you can start a virtual machine in a typical virtual machine GUI and then control that machine from the command line, or possibly remotely. VirtualBox also comes with a full Software Development Kit: even though it is Open Source Software, you don't have to hack the source to write a new interface for VirtualBox. Virtual machine descriptions in XML. The configuration settings of virtual machines are stored entirely in XML and are independent of the local machines. Virtual machine definitions can therefore easily be ported to other computers. VirtualBox 7.2.10 changelog: VMM: Fixed issue when CentOS 10 VM was not booting due to the message "Fatal glibc error: CPU does not support x86-64-v3" (​github:gh-642) Devices/EFI: Fixed booting issue when ARM VM had less than 1024 MiB of RAM assigned (​github:gh-679) USB: Fixed issue when it was not possible to attach USB device to headless VM on Apple Silicon/macOS 26.4.1 (​github:gh-631) Storage: Fixed issue when VIRTIO-SCSI device was not recognized as SSD device by guest system (​github:gh-634) Network: Fixed issue in E1000 emulation code which triggered debug log creation (​github:gh-645) Network: Fixed issue in E1000 emulation code which prevented OS/2 guest from booting (​github:gh-683) Linux Host: Fixed issue when VMs could not be started due to kernel oops (​github:gh-639) Linux Host and Guest: Fixed issue when kernel modules were failing to build with openSUSE 16.0 kernel Linux Host and Guest: Added initial support for kernel 7.1 Linux Host and Guest: Added extra fixes for RHEL 9.8 kernel (​github:gh-676) Linux Host and Guest: Added possibility to build source code using NASM instead of YASM as the assembler (​github:gh-520) Linux Guest Additions: Added initial support for Extended Data Control Protocol for clipboard sharing with Plasma on Wayland guests (​github:gh-33) Linux Guest Additions: Added extra fixes for preventing vboxvideo kernel module build with kernel version 7.0 and newer (​github:gh-655) OS/2 Guest Additions: Fixed issue when Shared Folders automount and clipboard sharing stopped working (​github:gh-551) Download: VirtualBox 7.2.10 | 170.0 MB (Open Source) Download: VirtualBox 7.2.10 Extension Pack | 19.1 MB View: VirtualBox Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • OK, now ask yourself how are they going to enforce that law? By requiring every single adult to prove their age and provide their legal identity documents to an UNREGULATED 3rd party company that already has a long track record of multiple data breaches. Not to mention, parliament have voted AGAINST this ban, twice, and Starmer is going ahead anyway. So, where's the democracy here, because that looks like dictatorship to me. The solution here is parental responsibility, not government control. Run some public service announcements on TV and UK social media teaching parents how to setup parental controls. That's already been proven to actually work. But the, this is not and has NEVER been about keeping kids safe. It's about control and monitoring. Watching what you're doing online and controlling what you can see and what you can say.
    • Interesting read. I knew the adware was quite controversial at the time, however never realised to the point The Guardian wrote an article about Patchou. I just said no and enjoyed his creation, I’d probably be a lot more wary of something like that today though.
  • Recent Achievements

    • One Month Later
      Prasann earned a badge
      One Month Later
    • Week One Done
      Prasann earned a badge
      Week One Done
    • First Post
      Dys Topia earned a badge
      First Post
    • Collaborator
      vjlex earned a badge
      Collaborator
    • Reacting Well
      Dys Topia earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      525
    2. 2
      +Edouard
      180
    3. 3
      PsYcHoKiLLa
      105
    4. 4
      Steven P.
      89
    5. 5
      ATLien_0
      70
  • Tell a friend

    Love Neowin? Tell a friend!