Run the Windows .net Application in System Tray on System Startup
Hi,
Today i have created a .net windows application which has following key points.
1. Run only one instance of the project:
to achieve this i have change the code of Program.cs as:
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- bool instanceCountOne = false;
- using (Mutex mtex = new Mutex(true, "MyRunningApp", out instanceCountOne))
- {
- if (instanceCountOne)
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- MainForm form = new MainForm();
- Application.Run();
- mtex.ReleaseMutex();
- }
- }
- }
- }
2. Run Application on minimize mode in system tray at first time.
here i also make some change in Application start as:
- MainForm form = new MainForm();
- Application.Run();
instead of
- Application.Run(new MainForm());
to run application on system tray by default i.e. in minimize mode on first time program start.
Note: to use Mutex you need to add System.Threading namespace.
3. Run Application at startup when System Start :
for this i have made changes in Main form at Initialize
- public MainForm()
- {
- InitializeComponent();
- this.Hide();
- Microsoft.Win32.RegistryKey rkApp = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
- // Check to see the current state (running at startup or not)
- if (rkApp.GetValue("MyApp") == null)
- {
- // The value doesn't exist, the application is not set to run at startup
- rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());
- }
- else
- {
- // The value exists, the application is set to run at startup
- rkApp.DeleteValue("MyApp", false);
- }
- }
4. On the form i am using
(a) Menu Strip.
(b) Notify Icon.
(c) Context Menu Strip.
(d) Timer.
and the whole page coding is as follow:
- private void minimizeToTaskbarToolStripMenuItem_Click(object sender, EventArgs e)
- {
- WindowState = FormWindowState.Minimized;
- }
- private void minimizeToSystemTrayToolStripMenuItem_Click(object sender, EventArgs e)
- {
- minimizetoTray();
- }
- private void minimizetoTray()
- {
- this.Hide();
- notifyIcon1.Visible = true;
- notifyIcon1.ContextMenuStrip = contextMenuStrip1;
- notifyIcon1.BalloonTipText = "Application is running on system tray";
- notifyIcon1.BalloonTipTitle = "Sample of System tray";
- notifyIcon1.ShowBalloonTip(200);
- }
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void exitApplicationToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void restoreToScreenToolStripMenuItem_Click(object sender, EventArgs e)
- {
- notifyIcon1.Visible = false;
- this.Visible = true;
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- lblTime.Text = DateTime.Now.ToString();
- timer1.Interval = 1000;
- timer1.Start();
- minimizetoTray();
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- lblTime.Text = DateTime.Now.ToString();
- timeToolStripMenuItem.Text = lblTime.Text;
- }
- private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- notifyIcon1.Visible = false;
- this.Show();
- this.WindowState = FormWindowState.Normal;
- }
you can download demo code with Setup project (Build Setup project to get exe for test)