NotifyIcon puts an icon in the Windows notification area (the system tray, next to the clock). Use it for apps that run in the background: a sync tool, a monitor, a form that minimizes to the tray instead of the taskbar.
NotifyIcon is part of WinForms, so it is Windows-only. Target net8.0-windows with <UseWindowsForms>true</UseWindowsForms>. WPF apps can use it too by adding <UseWindowsForms>true</UseWindowsForms> to their project.
The minimum
var trayIcon = new NotifyIcon
{
Icon = SystemIcons.Information,
Text = "New message",
Visible = true
};
trayIcon.ShowBalloonTip(3000, "Information", "A new message has arrived.", ToolTipIcon.Info);
Two properties are easy to forget. Without an Icon, nothing appears at all, and the icon only shows after you set Visible = true.
A tray-only app with a menu
An app that has no main window needs something to keep the message loop running. An ApplicationContext does that: Application.Run keeps going until the context calls ExitThread.
internal static class Program
{
[STAThread]
private static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new TrayApplicationContext());
}
}
internal sealed class TrayApplicationContext : ApplicationContext
{
private readonly NotifyIcon trayIcon;
public TrayApplicationContext()
{
var menu = new ContextMenuStrip();
menu.Items.Add("Show message", null, (_, _) => ShowMessage());
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add("Exit", null, (_, _) => ExitThread());
trayIcon = new NotifyIcon
{
Icon = SystemIcons.Application,
Text = "CSharpExamples Tray Demo",
ContextMenuStrip = menu,
Visible = true
};
trayIcon.DoubleClick += (_, _) => ShowMessage();
}
private void ShowMessage() =>
trayIcon.ShowBalloonTip(3000, "Information", "A new message has arrived.", ToolTipIcon.Info);
protected override void ExitThreadCore()
{
trayIcon.Visible = false;
trayIcon.Dispose();
base.ExitThreadCore();
}
}
Right-clicking the icon opens the menu; double-clicking it shows a notification. ApplicationConfiguration.Initialize() is generated by the WinForms project template in .NET 6 and later; on .NET Framework, call Application.EnableVisualStyles() and Application.SetCompatibleTextRenderingDefault(false) instead.
For your own icon, add an .ico file to the project and load it with new Icon("app.ico"), or reuse the executable’s icon with Icon.ExtractAssociatedIcon(Application.ExecutablePath).
Minimize a form to the tray
For a normal form that should disappear into the tray when minimized:
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (WindowState == FormWindowState.Minimized)
Hide();
}
private void TrayIcon_DoubleClick(object? sender, EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
Activate();
}
Hook TrayIcon_DoubleClick up to the NotifyIcon‘s DoubleClick event, in the designer or in the constructor.
Things that catch people out
The ghost icon. If the process exits without disposing the NotifyIcon, the icon stays in the tray until the user moves the mouse over it. Set Visible = false and call Dispose() on the way out, as ExitThreadCore does above. When you add the NotifyIcon in the designer, it is added to the form’s components container and disposed with the form.
Balloon tips are toast notifications now. On Windows 10 and 11, ShowBalloonTip shows a standard notification, which appears in Notification Center. The timeout argument is ignored; Windows decides how long it stays based on its accessibility settings. If the user has turned off notifications for the app or enabled Do Not Disturb, nothing is shown and your code gets no error.
The tooltip text has a length limit. On .NET Framework, setting Text to more than 63 characters throws ArgumentOutOfRangeException. Keep it short.
Windows 11 hides new icons. By default, a new tray icon lands in the overflow area behind the arrow. Users have to drag it out or enable it in Settings. There is no supported way for an app to promote itself.
Events arrive on the UI thread. Click handlers run on the thread that created the icon, so you can touch forms directly. If a background task needs to show a balloon, marshal back to the UI thread first; see updating a GUI element from another thread.
For richer notifications with buttons, images or input fields, look at the Windows App SDK’s app notifications instead of balloon tips.