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:

Code Snippet
  1. static class Program
  2.     {
  3.         /// <summary>
  4.         /// The main entry point for the application.
  5.         /// </summary>
  6.         [STAThread]
  7.         static void Main()
  8.         {
  9.             bool instanceCountOne = false;
  10.             using (Mutex mtex = new Mutex(true, "MyRunningApp", out instanceCountOne))
  11.             {
  12.                 if (instanceCountOne)
  13.                 {
  14.                     Application.EnableVisualStyles();
  15.                     Application.SetCompatibleTextRenderingDefault(false);
  16.                     MainForm form = new MainForm();
  17.                     Application.Run();
  18.                     mtex.ReleaseMutex();
  19.                 }               
  20.             }           
  21.         }
  22.     }

2. Run Application on minimize mode in system tray at first time.

here i also make some change in Application start as:

 

Code Snippet
  1. MainForm form = new MainForm();
  2.                     Application.Run();

instead of

Code Snippet
  1. 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

Code Snippet
  1. public MainForm()
  2.         {
  3.             InitializeComponent();
  4.             this.Hide();            
  5.             Microsoft.Win32.RegistryKey rkApp = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  6.             // Check to see the current state (running at startup or not)
  7.             if (rkApp.GetValue("MyApp") == null)
  8.             {
  9.                 // The value doesn't exist, the application is not set to run at startup
  10.                 rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());
  11.             }
  12.             else
  13.             {
  14.                 // The value exists, the application is set to run at startup
  15.                 rkApp.DeleteValue("MyApp", false);
  16.             }
  17.         }

 

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:

Code Snippet
  1. private void minimizeToTaskbarToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             WindowState = FormWindowState.Minimized;
  4.         }
  5.  
  6.         private void minimizeToSystemTrayToolStripMenuItem_Click(object sender, EventArgs e)
  7.         {
  8.             minimizetoTray();
  9.         }
  10.         private void minimizetoTray()
  11.         {
  12.             this.Hide();
  13.             notifyIcon1.Visible = true;
  14.             notifyIcon1.ContextMenuStrip = contextMenuStrip1;
  15.             notifyIcon1.BalloonTipText = "Application is running on system tray";
  16.             notifyIcon1.BalloonTipTitle = "Sample of System tray";
  17.             notifyIcon1.ShowBalloonTip(200);            
  18.         }
  19.  
  20.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  21.         {
  22.             Application.Exit();
  23.         }
  24.  
  25.         private void exitApplicationToolStripMenuItem_Click(object sender, EventArgs e)
  26.         {
  27.             Application.Exit();
  28.         }
  29.  
  30.         private void restoreToScreenToolStripMenuItem_Click(object sender, EventArgs e)
  31.         {
  32.             notifyIcon1.Visible = false;
  33.             this.Visible = true;
  34.         }       
  35.  
  36.         private void MainForm_Load(object sender, EventArgs e)
  37.         {
  38.             lblTime.Text = DateTime.Now.ToString();
  39.             timer1.Interval = 1000;
  40.             timer1.Start();
  41.             minimizetoTray();            
  42.         }
  43.  
  44.         private void timer1_Tick(object sender, EventArgs e)
  45.         {
  46.             lblTime.Text = DateTime.Now.ToString();
  47.             timeToolStripMenuItem.Text = lblTime.Text;           
  48.         }
  49.  
  50.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  51.         {         
  52.             notifyIcon1.Visible = false;
  53.             this.Show();
  54.             this.WindowState = FormWindowState.Normal;
  55.         }

 

you can download demo code with Setup project (Build Setup project to get exe for test)

Demo Download

1 Comment

Comments have been disabled for this content.