Search code examples
c#.netautostarttrayicon

Autostart application and open it in taskbar with tray icon


I have an application in .net where I want it to always open without any manual interaction.

In this application I have used NotifyIcon so it always start in Taskbar tray but the notification icon will only show if I manually open that .exe.

so what I did is just added it in Autostart application registry entry with the help of below:

RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());

So this works fine and on reboot it successfully open it in system taskbar Process list but not as a taskbar tray icon.

Can anyone help me.?


Solution

  • There was an issue with the way I was using the icon.

    There might be an issue with the "icon" file we use in NotifyIcon so I just fixed that issue just by replacing the way

    // START: Creating a red graphic instead of image
    Bitmap b = new Bitmap(16, 16);
    
    Graphics g = Graphics.FromImage(b);
    g.Clear(Color.Transparent);
    SolidBrush sb = new SolidBrush(Color.Red);
    g.FillEllipse(sb, 0, 0, 16, 16);
    // END: Creating a red graphic instead of image
    
    m_notifyicon.Visible = true;
    m_notifyicon.Icon = Icon.FromHandle(b.GetHicon());
    

    Now I am able to see the Red icon even after rebooting my computer.