Set system tray notifyicon.icon to a pic in images folder

11,845

After adding the picture file to your project, bring up the item properties by clicking on it, and pressing F4. Under Build Action, change it to "Embedded Resource".

You can access embedded resources in Stream form like this:

public FolderMonitorApplicationContext()
{
    this.monitor =  new Monitor();

    notifyIcon = new NotifyIcon();
    using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
        "<project namespace>.<folder path>" + "filename.ico"))
    {
        notifyIcon.Icon = new Icon(stream);
    }

    notifyIcon.Text = "Folder Monitor";
    notifyIcon.Visible = true;

    contextMenu = new ContextMenuStrip();
    openMonitor = new ToolStripMenuItem();
    exitApplication = new ToolStripMenuItem();

    notifyIcon.ContextMenuStrip = contextMenu;

    notifyIcon.DoubleClick += NotifyIcon_DoubleClick;

    openMonitor.Text = "Open";
    openMonitor.Click += new EventHandler(OpenMonitor_Click);
    contextMenu.Items.Add(openMonitor);

    exitApplication.Text = "Exit..";
    exitApplication.Click += new EventHandler(ExitApplication_Click);
    contextMenu.Items.Add(exitApplication);
}
Share:
11,845
Rob
Author by

Rob

Updated on June 04, 2022

Comments

  • Rob
    Rob almost 2 years

    I've tried several things and eventualy just put the image directly inside C:\Users\Gebruiker\Documents\Visual Studio 2012\Projects\FolderMonitor\FolderMonitor\bin\Debug. This works for now, but idealy I'd like to set the notifyIcon.Icon = new Icon("folder.ico") to an image inside a images folder in the solution. But I can't figure out how this works..

        public FolderMonitorApplicationContext()
        {
            this.monitor =  new Monitor();
    
            notifyIcon = new NotifyIcon();
            notifyIcon.Icon = new Icon("folder.ico");
            notifyIcon.Text = "Folder Monitor";
            notifyIcon.Visible = true;
    
            contextMenu = new ContextMenuStrip();
            openMonitor = new ToolStripMenuItem();
            exitApplication = new ToolStripMenuItem();
    
            notifyIcon.ContextMenuStrip = contextMenu;
    
            notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
    
            openMonitor.Text = "Open";
            openMonitor.Click += new EventHandler(OpenMonitor_Click);
            contextMenu.Items.Add(openMonitor);
    
            exitApplication.Text = "Exit..";
            exitApplication.Click += new EventHandler(ExitApplication_Click);
            contextMenu.Items.Add(exitApplication);
        }
    

    So it's currently working, but not how it i'd like it to work. Hope you can help me out here, thanks in advance.

  • Rob
    Rob almost 11 years
    Thanks, this solved my problem!:) I've tried this before but forgot to change the property on the image..
  • Idle_Mind
    Idle_Mind almost 11 years
    You can shorten that to notifyIcon.Icon = new Icon(this.GetType(), "folder.ico");
  • Alexandru Dicu
    Alexandru Dicu over 2 years
    @Idle_Mind This works only if folder.ico stays next to .cs file, in the same folder. However, people in general tend to put these icons into a Resources/Images folder to keep things organized, not next to CS files.