1

So I have a really frustrating problem. I have a WPF application written in C# running Windows 10 (version 1703, OS Build 15063.0) that creates shortcuts for the current running executable. The problem is, when I create these, I set a different (custom) icon for the shortcuts. I didn't think this would be an issue, but the next time I run the application (from the original location) it displays the custom icon which I set for the shortcut in the taskbar. Just the taskbar. The title bar and task manager show the correct icon and title. What am I doing wrong here? At this point I think it's a Windows bug. My method for creating the shortcuts is below:

public void CreateShortcut(string name)
{
        string AppDir = GetAppDataDir();
        object shDesktop = (object)"Desktop";
        WshShell shell = new WshShell();
        string shortcutAddress = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + name + ".lnk";
        IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
        shortcut.Description = "Play " + name + "!!";
        shortcut.Hotkey = "Ctrl+Shift+N";
        shortcut.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        shortcut.TargetPath = Directory.GetCurrentDirectory() + "/Catapult.exe";
        shortcut.Arguments = "-play \"" + name + "\"";
        shortcut.IconLocation = AppDir + "/Games/" + name + ".ico";
        shortcut.Save();
}
Luke Dinkler
  • 731
  • 5
  • 16
  • 36
  • it will be better if you share some code, and also specify which OS you are using and so on – Yogesh H Shenoy Nov 04 '17 at 14:10
  • The icon show in the titlebar and task manager is provided by the process itself. The icon shown in the taskbar is provided by explorer. When launching a process, there is no predetermined way to override the icon that the process knows about. If you are developer of the process, you can use a command line parameter. – Joel Lucsy Nov 07 '17 at 14:43
  • @JoelLucsy Use a command line parameter for what? Changing the taskbar icon back? – Luke Dinkler Nov 07 '17 at 14:47
  • Now I'm confused. Are you wanting the taskbar to be the icon the program manages itself, or the overriden value you provided in the shortcut? I was assuming the latter. If you want the former, then simple set the icon to the exe when creating the shortcut. – Joel Lucsy Nov 07 '17 at 14:50
  • @JoelLucsy I need the program to show the correct icon (it's own) in the taskbar. This is currently not happening because there is a shortcut referencing the executable with a different icon. – Luke Dinkler Nov 07 '17 at 14:52
  • Then its the shortcut that needs to change. – Joel Lucsy Nov 07 '17 at 14:54
  • @JoelLucsy But I need the shortcut to have a custom icon. Is this impossible? – Luke Dinkler Nov 07 '17 at 14:55
  • Explorer is going to use the icon provided in the shortcut for the icon in the taskbar. You can also see this referenced at https://stackoverflow.com/questions/7111803/taskbar-icon-will-not-change-once-binary-is-installed – Joel Lucsy Nov 07 '17 at 15:00
  • Only thing you might be able to do is to have the shortcut point to a batch file which launches your program. This will kill the link between whats is run and the shortcut points to. – Joel Lucsy Nov 07 '17 at 15:01
  • @JoelLucsy This was the original design, but it's so much more clunky. This is poor design on Microsoft's part with Explorer. The icon shown in the task bar should be found in the executable, not some shortcut that has the same path. Thanks anyway. – Luke Dinkler Nov 07 '17 at 15:04
  • I guess it is done to account for the fact that a user changing the icon for a shortcut on their desktop would want the task bar icon to match whatever they changed it to. It's probably written to work from the user's perspective, not the application developer's. – Bradley Uffner Nov 07 '17 at 16:05
  • @BradleyUffner Yeah, I suppose. They should still take into account whether the app is being launched directly from the executable or from the customized shortcut though. – Luke Dinkler Nov 07 '17 at 16:07
  • It looks like you can get access to change the icon for the running process via pInvoke. https://stackoverflow.com/questions/18493442/how-to-change-the-icon-in-taskbar-using-windows-api – Bradley Uffner Nov 07 '17 at 16:07
  • @BradleyUffner That code is for a standard C++ program – Luke Dinkler Nov 07 '17 at 17:09

1 Answers1

0

It would appear that my solution is that there is no solution. In its current state, Windows does not allow you to create shortcuts with custom icons that reference an executable without messing up the existing icon for that executable. To me, this is unacceptable as I now have to use clunky code to get around this (nothing worse than that, am I right?). Microsoft needs to fix this quirky implementation in Explorer ASAP.

Luke Dinkler
  • 731
  • 5
  • 16
  • 36