Click here to Skip to main content
15,439,117 members
Please Sign up or sign in to vote.
2.25/5 (4 votes)
I have a Windows 7 application where I need to change the icon displayed on the main form and the icon that is displayed by Windows on the taskbar at runtime.

In code I am simply changing the Form.Icon property and this works no problem if I run the app from the EXE. The icon changes in the form and on the taskbar no problem.

However, if I run the app from a desktop shortcut the icon only changes in the main form and the icon shown on the taskbar never changes. Apparently Windows is loading the taskbar icon from a cache. The only suggestions I can find on how to refresh the cached taskbar icon are to delete the iconcache and then restart windows. This solution obviously will not work for changing the icon at runtime.

Does any one know how change the taskbar icon at runtime for an app launched via a desktop shortcut??? I am guessing there are some api functions that will work, but I cannot figure it out.
Updated 24-Feb-21 5:36am
Sergey Alexandrovich Kryukov 29-May-13 15:50pm     CRLF
Somebody already asked a similar question recently, for deployment time. No one explained to me: why? Now, why during runtime? —SA 29-May-13 16:04pm    
The app I am building allows the user to switch "accounts" at runtime. When the account is switched the icon that is displayed in the taskbar icon needs to change to give the user a visual indication (on the taskbar) of which account is selected in the running app.
Sergey Alexandrovich Kryukov 29-May-13 16:11pm     CRLF
I answered. I would advice to change the window icon only (or, much better, only the part of the icon: the user should supply an icon with transparency, and you could superimpose it as a foreground, with the unmodified background). I don't think changing the application icon would make sense; and it's impossible; I tried to explain why. —SA 29-May-13 16:19pm    
Why would it work fine when there is no shortcut targeting the EXE? It only becomes a problem after creating a shortcut for the EXE.
Sergey Alexandrovich Kryukov 29-May-13 16:14pm     CRLF
By the way, if you want to allow multiple instances of the application, Window 7 show then nicely, under the common application icon. When you put a mouse cursor over it, you got separate main windows of thumbnail size, with their (different) form icons. Isn't it a better idea? —SA

I thought of a very easy work-around that might work for others. My underlying problem was due to how Windows caches the icons of application shortcuts. This was preventing me from changing the taskbar icon at runtime. Which is certainly possible. So instead of having the start shortcut pointing directly at the main application exe, I am pointing the shortcut to a launcher application that shells out to the main application. This way, the main application exe is never launched from a shortcut. Thus, the icon on the taskbar can be changed at runtime. This is working very well for me.
Share this answer
D3athRace 20-Jul-17 1:24am    
Can you provide a sample of how you did it? It would be a great help.
Luc Pattyn 2-Jul-20 20:34pm    
FYI: I recently learned Windows 10 does it differently: the taskbar icon does not depend on the application icon, instead it matches the one in the currently active form.
chj124 15-Jun-22 8:41am     CRLF
sample: In c# create a console app that uses Process.Start Must have: using System.Diagnostics; Then 1 line of code: Process.Start(appName);
I am having the exact same issue but I never realized that it was happening because of a "Shortcut". I noticed this behavior when I would pin my application to the windows 10 task bar. If I don't pin the application, I can simply change the taskbar icon by using {this.Icon = new Icon("my icon path")} and it works as expected. Once I pin the application, the icon will not change. As the original poster mentioned, this also seems to happen if you create a shortcut to the application (and not pin it to the taskbar).

None of the proposed solutions above seem to actually solve the issue and the "chosen solution" was a hack of a work around by someone who was in desperate need of a solution but was not offered any.

Unfortunately this is also not a solution but I am attempting to revive this issue as it is still unresolved but the original poster has made a connection that could be key to solving this (a shortcut to the application prevents the taskbar icon from changing).

There also seems to be some unhelpful comments by a few individuals claiming that this has been solved, which it hasn't (only a workaround provided by the original poster), or that it's impossible, which it isn't since there are many examples of applications that change the taskbar icon as a form of notification (outlook when you have unread emails or express vpn when you are connected to a VPN).

The issues as far as I can tell revolves specifically around windows behavior when a shortcut to an application exists (or pinned to taskbar which I assume is creating a shortcut somewhere). Without the shortcut, it is very much possible for the taskbar icon to be changes as I mentioned above.

Is anyone familiar enough with this specific windows behavior to help overcome this challenge?
Share this answer
Please see my comment to the question. Why?

Now, just some notes. The form icon is a different thing, you can really change it easily during runtime. It may even make some sense (different mode of operation, different part of functionality). The icon shown in the Taskbar is a different thing, it is called application icon. As OS uses it, it should be stored in the PE module (file) in some universal way, not specific to .NET. The application icon is actually build in application manifest. For an assembly, this is stored in a module, the one holding an application manifest for the assembly. (Visual Studio supports only creation of the assembles with one module per assembly, but compiling on lower level will allow you to have more then one per assembly, but only one of them will hold the manifest; it's called "main assembly module".)

The MSBuild uses the icon file in this build step:[^].

That said, unlike the form icon, this icon is readonly. And, as you should know, the executable module loaded for execution cannot be modified, no matter how high your permissions are. This is an important protection feature of most modern OS.

And even if you could change this icon by some means (no, I don't think it's possible), it would defeat the purpose of them. OS controls rely on this icon to keep your application well-recognizable; it would not get any notifications if you could change it, because this is not how things are designed.

Share this answer
Comments 29-May-13 16:15pm    
What I am trying to do works just fine when there is no shortcut to the EXE. As soon as a shortcut is created targeting the EXE, the taskbar icon stops updating at runtime. I would rather not get into a philosophical debate about why I should not try to do this. Instead, I would just like to know if anyone has done this successfully. And if so how. Thanks!
Sergey Alexandrovich Kryukov 29-May-13 16:26pm     CRLF
It won't be "done successfully", I tried to explain why. And nobody debates with you, why would I? Are you accepting the answer formally (green button) or not? —SA 29-May-13 16:37pm     CRLF
You have not provided a solution. Rather you are suggesting alternatives. Therefore I would not consider your response a "solution". At this point I disagree that it is impossible. So I will continue trying to figure it out. Thanks.
Sergey Alexandrovich Kryukov 29-May-13 16:57pm     CRLF
What do you want if the solution is impossible. And the alternative is not "just". Of course, it's totally up to you. Especially if you disagree. I still don't understand what makes you thinking that this is possible. Could you explain? —SA 29-May-13 16:43pm    
If what I am asking about is impossible, please explain why it works just fine if I start the application by running the EXE directly. The app does exactly what I want and the icon changes at runtime in the taskbar! It only becomes a problem after creating a shortcut to the EXE.
Application won't show you taskbar in runtime until you have any shortcuts to it.
If you get rid of all shortcuts and run from Runtime folder, it will work fine...
But this is not what we need.

nobody anywhere didn't provide for solution what... what a shame....
Share this answer
Richard Deeming 3-Jul-20 7:01am    
This question was solved seven years ago. Your complaint that the solution didn't fix a different question is not a "solution" to this already-solved question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900