Changing a desktop icon's image in C#

pcnerd37

Senior member
Sep 20, 2004
944
0
71
I've been recruited to do a project where they want a programs desktop icon to change based on various conditions. What I need to know is if it is possible for a background process to change a desktop icon's image when it needs to and how I would go about doing such a thing.

I have never seen anybody do something like this before so I don't even know if its possible, but if it is, I am hoping that somebody here might know how to do it. I should also mention that this is for Windows, but eventually I will need to know how to do it on other systems such as the Mac, so if you know how to do it with the Mac, I would like to hear about that as well. Thanks in advance!
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Sounds like you should tell them about something called the "system tray" -- that's where dynamically-changing icons normally run free :)

On the Mac, they need to learn about "the dock."


Otherwise, you'd need to read how to change the shortcut files in the Desktop folder. I know installers allow associating any icon you want (such as .ico files) with a shortcut, you'd need to google how this works and duplicate it.

One problem you'll run into is with Vista: your background process probably won't have sufficient rights to alter shortcuts unless it's a service, and maybe not even then.


Really, the system tray and dock are the right way to do this.
 

pcnerd37

Senior member
Sep 20, 2004
944
0
71
I thought the system tray myself, but apparently that is too small for what she is wanting so she wants to do it with an icon. I'm going to try to pitch the idea of a widget to her but if she doesn't go for it, I should be up to speed on how to do this.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
> Widget

Vista has that sidebar thing I immediately turn off, it could go there.

In any version of Windows a process can have a window that tries to make itself always on top (see sticky note software).

CodeProject.com should have examples of both.

Or: system tray icons can have some pretty long plain text for their tooltips, my wifi adapter shows 4 lines of text. It might also be possible to override the tooltip to do something with graphics, maybe (never tried it).
 

sao123

Lifer
May 27, 2002
12,653
205
106
well... the desktop icon for any given exe, that relationship is stored in the registry.
of course, there must be the proper resource files to provide the icons.
but all you would have to do, is have a background service running, which would then change the registry key to point it to the proper icon, and then force a screen refresh.

but, uh, i think this is a little deeper than you want to get.

 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Point the shortcut to c:\myicon.ico and just update c:\myicon.ico when you want, along with the icon shell cache.

And AFAIK, the location of the icon is stored in the .lnk shortcut file.

Also check this out. http://support.microsoft.com/kb/179904

Use PInvoke to use Win32 API thru C#.

If it's the same set of shortcuts (say, 4 different ones) each time you could also just make 4 shortcut files and rotate them onto the desktop whenever it needs to be. No use in making things overcomplicated.

On Mac it might be easier to change the icons if they use simple link files like Linux does.

And using the system tray is a better idea because icons can be cached and it's sometimes hard to guarantee and refresh them.