• 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

    • Nearly half of American adults now use AI, but concerns are also growing by Hamid Ganji Since the launch of ChatGPT in 2022, the way people research, get their news, and perform routine tasks has changed dramatically. Now, almost everything around us has a touch of AI, and companies are trying to embed it into nearly every product and service they offer. With that in mind, new research shows how Americans are actually adopting this change and using AI in their everyday lives. According to new research conducted by the Pew Research Center, 49% of American adults now use AI chatbots like ChatGPT or Gemini. This marks a significant increase over last year, when only 33% of American adults reported using AI. Additionally, four in ten U.S. adults (42%) said they use AI tools to research information, while 38% said they use these tools to handle tasks at work. Entertainment, image and video editing, and getting medical advice are among the other ways Americans are using AI. Moreover, ChatGPT dominates the U.S. AI market, with 44% of respondents saying they use OpenAI's chatbot. Gemini follows at 24%, while Copilot and Meta AI account for 17% and 14%, respectively. Respondents also said that AI chatbots generally have a positive impact on their productivity and how informed they are. But when it comes to AI’s impact on society, Americans remain largely skeptical. About 40% of American adults believe AI will be more harmful than beneficial to society over the next 20 years. Additionally, 31% expect AI to have a negative effect on them personally. Another 31% of respondents say AI could be equally positive and negative. As for data security, pessimism remains high: 71% of respondents say AI will make their personal information less secure, while only 3% believe it will make their data more secure. American adults also largely lack confidence in both the government and AI companies when it comes to regulating and developing AI. About 67% of Americans have little to no confidence in the U.S. government’s ability to regulate AI effectively. Six in ten adults are also not confident that U.S. companies will develop and use these tools responsibly.
    • MultiOS-USB 0.11.1 by Razvan Serea MultiOS-USB is a versatile, open-source utility designed to create multiboot USB drives capable of hosting multiple operating systems on a single portable device. The project simplifies the process of building a bootable USB by automating the configuration of various boot loaders and file systems, enabling users to install and run diverse operating systems, including Windows, Linux distributions, and diagnostic tools, directly from one drive. It supports ISO booting and persistence, which allows changes made during live sessions to be retained, making it ideal for testing, troubleshooting, or system recovery. Features: BIOS and UEFI support Secure Boot support (boot, manage uefi keys) Load UEFI drivers Launch .efi executables and other boot loaders Boot Linux from .iso images Boot WinPE from bootable .wim images Boot Windows 10/11 installer from ISO (currently, SB must be disabled during installation) Boot Linux installer from network (experimental) Boot locally installed systems: Linux, Windows Automatically update configuration files Without background services exFAT file system support Automatic detection of compatible ISO images (GRUB loopback) Support for systems without loopback support Allows customisation of ISO boot menu (for example: custom kernel options) Support for USB, SSD, nvme, mmcblk, loop, nbd and virtual disks Support for x86, x86_64 A list of tested ISO images can be found here MultiOS-USB 0.11.1 changelog: 68122b7: Fixed-release AUR package #63 fba0283: Update shim to 16.1 8c2ae95: Update grub to v2.14-1 ea15c1d: Update Memtest86+ to v8.10 162f4e6: Add secureblue (#71) b2da8ae: Add AerynOS (#74) ac6640e: Bump config.version 34e9ca6: Add Bluefin (#72) 7a10edd: Add Aurora (#66) cab701b: Update wimboot to v2.9.0-1 90da7f7: Fix Windows error: 0x80070001 - 0x4002F (#52) 2dea73d: Add Microsoft certificates 01f479e: Remove old efi_uga module Download: MultiOS-USB 0.11.1 | 5.3 MB (Open Source) View: MultiOS-USB Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Latest Rufus update improves new Windows 11 install method by Taras Buria Pete Batard, the maker of Rufus, a very popular app for creating bootable Windows (and other OS) media, has released a new beta version of its app. Rufus 4.15 beta is now out, and while it offers no new features, there are all sorts of improvements and fixes, including for the new Windows 11 installation method that was introduced in version 4.14 in early May. The "Silent Windows 11 installation" is a new feature whose goal is to automate operating system installation. All you have to do is boot from the drive, and then Rufus takes over, doing all things for you, such as setting up a new account, skipping ads and prompts, and more. It is a very handy tool, but initially, it had some bugs and issues that required addressing. With version 4.15 beta, Rufus is fixing that, particularly a bug with installation failing at 75%, crashes on Snapdragon X-based PCs, and more. Here is the changelog: Rufus 4.15 beta is now available for download from its GitHub repository. If you have never used Rufus before, you can check out our guide here. It is a very useful utility to have, as it allows you to deal with plenty of Windows 11's annoyances, which are still there, despite Microsoft's ongoing efforts to fix them.
    • Microsoft fixes one of Excel Copilot's most frustrating limitations by Usama Jawad Microsoft began integrating Copilot into Excel a couple of years ago and has been upgrading it with new functionalities since then. While some changes have been controversial, Microsoft is hoping to win over users by allowing them to be more productive via Copilot. To that end, it has now announced a Copilot improvement that may actually be appreciated by people who use it regularly. Excel customers often use the Copilot prompt box to issue instructions to format and customize their data, but it can become quite tiring to keep repeating the same instructions again and again. Microsoft now allows you to define Copilot personalization rules for formatting, naming conventions, formulas, and report styles. These can be accessed via Settings > Personalization, where you can explain your rules in natural language like "Always format currency in USD with no decimals", and just let Copilot take care of the rest. Microsoft is going a step further in this direction by allowing you to set workbook rules too. These rules are stored as a .Rules sheet, and are preserved while the workbook is shared. This fosters collaboration while making sure that standard rules govern the Copilot editing experience across the organization. Other advantages of this capability include pointing it to specific examples, defining dynamic formulas, and referencing an entire sheet and asking Copilot to infer rules based on that. You can leverage this feature by opening Copilot in Excel, clicking on "+", and selecting Create workbook rules. If you have an existing .Rules sheet, you can simply start listing the rules in column A as well. Personalization features are available to all Copilot in Excel users across the web, Mac, and Windows. Meanwhile, workbook rules are currently being previewed for Windows and Mac customers on the Insiders channel. General availability is scheduled after a few weeks, but a concrete date is currently unknown. Overall, the Excel capability is quite similar to ChatGPT's memory features, which allow you to permanently store items in the AI model's context window.
  • Recent Achievements

    • One Month Later
      Vincian earned a badge
      One Month Later
    • First Post
      Jocimo earned a badge
      First Post
    • Week One Done
      suprememobiles48 earned a badge
      Week One Done
    • One Month Later
      Windows Guy earned a badge
      One Month Later
    • One Month Later
      Prasann earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      172
    3. 3
      PsYcHoKiLLa
      90
    4. 4
      Steven P.
      76
    5. 5
      neufuse
      69
  • Tell a friend

    Love Neowin? Tell a friend!