Why does WPF NotifyIcon Icon set setter throw an exception?

I'm having problems with NotifyIcon in WPF, the second line throws an exception. I can not find a way to use the icon file that I have in the resources, can someone help.

notifyI = new NotifyIcon();
notifyI.Icon = new Icon("Power.ico");
notifyI.Text = "Shutdown Timer";
notifyI.Visible = true;
notifyI.MouseDoubleClick += new
System.Windows.Forms.MouseEventHandler(notifyI_MouseDoubleClick);
+3
source share
1 answer

The Icon (string) constructor searches for a file on disk for the icon file; it does not look in the resource. Instead, consider using the Icon (Stream) constructor.

Or use the tab "Project + Properties", the tab "Resource", the arrow on the button "Add resource", "Add existing file". Select the .ico file. Then you will use it as follows:

 notifyI.Icon = Properties.Resources.Power;
+14

Source: https://habr.com/ru/post/1747676/


All Articles