Accessing resources from code for setting NotifyIcon.Icon

Cheetah picture Cheetah · Dec 4, 2013 · Viewed 15.5k times · Source

I am trying to get the Icon of a NotifyIcon in WPF.

So I have added a .ico file to my solution in a Resources folder and set the build action to Resource.

I am trying to grab this resource in code behind like so:

var icon = (Icon) Application.Current.FindResource("/Resources/icon.ico")

This doesn't work.

In addition to this: Application.Current.Resources.Count returns 0.

EDIT

var i = new Icon(Application.GetResourceStream(new Uri("/systemtrayicon.ico", UriKind.Relative)).Stream);

With the icon in the root and the build action set to Resource.

Still not working.

EDIT AGAIN:

I needed to Clean the solution and rebuild as per: WPF throws "Cannot locate resource" exception when loading the image

Answer

Shivam Srivastava picture Shivam Srivastava · Dec 28, 2013

This will works 100%

ni.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,<Image Location From root>")).Stream);

Example:

notify.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,/images/favicon.ico")).Stream);