-1

I'm trying to set the icon of my application (visible in the taskbar). The icon is correct when I run the .exe itself or run it from visual studio, but this doesn't apply when starting from a shortcut.. The icon inside the application itself, top left corner, is correct.

Code used to set the icon:

var assemblyDirectory = Directory.GetCurrentDirectory();
var iconUri = new Uri(Path.Combine(assemblyDirectory, resourceName), UriKind.Absolute);
Icon= BitmapFrame.Create(iconUri);

I'm trying to add a red circle indicating changes in my application to the users, which is done by changing between 2 icons.

Any idea on how to set the icon of a shortcut at runtime, or about how to show a red circle in the taskbar-icon indicating changes?

Illedan
  • 414
  • 4
  • 14
  • Possible duplicate of [How do I get the path of the assembly the code is in?](https://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in) – mjwills Feb 22 '18 at 12:02
  • It's not. There is no problem in getting path of the assembly. Problem is setting the icon. – Illedan Feb 22 '18 at 12:04
  • Something special abut your shortcut? i.e. Does it start the .exe directly or perhaps via `runas` to have it started as a different user? – LocEngineer Feb 22 '18 at 12:05
  • The icon is set correctly inside the application when starting from a shortcut. But this icon is not shown in the taskbar, only inside the application itself. – Illedan Feb 22 '18 at 12:06
  • Shortcut is made from rightclicking the .exe and selecting create shortcut – Illedan Feb 22 '18 at 12:07
  • Well, with the code you do set the icon - that of the form. This is WinForms, right? You need to set the icon for the assembly, in your project's properties. – LocEngineer Feb 22 '18 at 12:08
  • Assembly icon != Form Icon – slow Feb 22 '18 at 12:08
  • I want this icon to change during runtime. Meaning a change in project's properties will not help.. – Illedan Feb 22 '18 at 12:10
  • 1
    Not possible, AFAIK. Unlike the form icon, the application Icon is read-only. – LocEngineer Feb 22 '18 at 12:12
  • Explorer has probably seen your program running earlier, before you gave it an icon. And remembered that it didn't have one. Google "reset shell icon cache", try running it on another machine. – Hans Passant Feb 22 '18 at 12:14
  • 1
    Would it be enough for you if you could change the icon as displayed in the taskbar, regardless of the shortcut icon? – LocEngineer Feb 22 '18 at 12:29
  • Sorry. I meant "tray", not "taskbar". Speaking of a NotifyIcon. – LocEngineer Feb 22 '18 at 12:36
  • Yes, it's enough to change the icon in the tray. – Illedan Feb 22 '18 at 13:18
  • See a good example here: https://www.codeproject.com/Tips/627796/Doing-a-NotifyIcon-program-the-right-way – LocEngineer Feb 22 '18 at 13:33

1 Answers1

0

Although it is impossible to set the assembly icon during runtime (it is read-only), you can use NotifyIcon to display an icon in system tray and that is fairly easy to change during runtime.

Here is an example of how to implement a NotifyIcon: https://www.codeproject.com/Tips/627796/Doing-a-NotifyIcon-program-the-right-way

Note: Since you are using WPF, you need to include a reference to Windows.Forms in your application.

Edit: just in case the link goes dead, here some code example for Program.cs, needs an "icon" imported as resource:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Application.Run(new MyCustomApplicationContext());
}

public class MyCustomApplicationContext : ApplicationContext
{
    public static NotifyIcon trayIcon;

    public MyCustomApplicationContext()
    {
        trayIcon = new NotifyIcon()
        {
            Icon = icon,
            ContextMenu = new ContextMenu(new MenuItem[]
            {
                new MenuItem("Exit", Exit),
                new MenuItem("Do something", DoSomething),
            }),
            Visible = true,
            BalloonTipText = "My Application",
            BalloonTipTitle = "My Application",
            Text = "My Application Text"
        };
        trayIcon.Click += new System.EventHandler(trayIcon_Click);
    }

    private void trayIcon_Click(object sender, System.EventArgs e)
    {
        //Do something
    }

    void DoSomething(object sender, EventArgs e)
    {
    }

    void Exit(object sender, EventArgs e)
    {
        // Hide tray icon, otherwise it will remain shown until user mouses over it
        trayIcon.Visible = false;

        Application.Exit();
    }
}
LocEngineer
  • 2,847
  • 1
  • 16
  • 28