SoFunction
Updated on 2025-03-08

How to set up C# program startup items

This article shares the setting method of C# program startup item for your reference. The specific content is as follows

Pallet icons set

Create a new NotifyIcon and an icon will be displayed on the tray.

You can set an ICO picture directly or use the icons of the original program.

= ();

public partial class MainWindow : Window
 {
  private NotifyIcon notifyIcon;

  public MainWindow()
  {
   InitializeComponent();
   SetNotifyIcon();
   ();
  }

  #region NotifyIcon

  private void SetNotifyIcon()
  {
    = new NotifyIcon();
    = "Disk Cleanup Tool";
   (2000);
    = "Disk Cleanup Tool: Clean it every 20 days";
    = ();
    = true;
   //Open menu item   MenuItem open = new MenuItem("Open");
    += new EventHandler(Show);
   //Exit menu item   MenuItem exit = new MenuItem("quit");
    += new EventHandler(Close);
   //Associate tray control   MenuItem[] childen = new MenuItem[] { open, exit };
    = new ContextMenu(childen);

    += new MouseEventHandler((o, e) =>
   {
    if ( == ) (o, e);
   });
  }

  private void Show(object sender, EventArgs e)
  {
    = ;
    = true;
   ();
  }

  private void Hide(object sender, EventArgs e)
  {
    = false;
    = ;
  }

  private void Close(object sender, EventArgs e)
  {
   ();
  }

  #endregion

  #region window
  private void MinimizeButton_OnClick(object sender, RoutedEventArgs e)
  {
   WindowState = ;
  }

  private void CloseButton_OnClick(object sender, RoutedEventArgs e)
  {
   ();
  }

  private void HeaderGrid_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  {
   if ( == )
   {
    ();
   }
  }

  #endregion
 }

Disable multi-process startup

//Double processes are prohibited bool canCreateNew;
 using ( m = new (true, , out canCreateNew))
 {
  if (!canCreateNew)
  {
   ();
  }
 }

Delete the original process

/// <summary>
 /// Delete the original process /// </summary>
 /// <param name="processName"></param>
 private void KillProcess(string processName)
 {
  //Get all open processes  try
  {
   Process currentProcess = ();
   var processes = (processName).Where(process=> !=);
   foreach (Process thisproc in processes)
   {
    //Find the program process and kill it.    if (!())
    {
     ();
    }
   }
  }
  catch (Exception ex)
  {
    
  }
 }

Set up the power-on self-start

private void SetAppAutoRun(bool autoRun)
 {
  if (autoRun) //Set the power-on start-up  {
   string path = ;
   RegistryKey rk = ;
   RegistryKey rk2 = (@"Software\Microsoft\Windows\CurrentVersion\Run");
   ("JcShutdown", path);
   ();
   ();
  }
  else //Cancel the power-on self-start  {
   RegistryKey rk = ;
   RegistryKey rk2 = (@"Software\Microsoft\Windows\CurrentVersion\Run");
   ("JcShutdown", false);
   ();
   ();
  }
 }

Complete code in:

public partial class App : Application
 {
  public App()
  {
   //Double processes are prohibited   bool canCreateNew;
   using ( m = new (true, , out canCreateNew))
   {
    if (!canCreateNew)
    {
     ();
    }
   }

   SetAppAutoRun(true);

   Startup += App_Startup;
  }

  private void SetAppAutoRun(bool autoRun)
  {
   if (autoRun) //Set the power-on start-up   {
    ("Set up the startup and need to modify the registry", "hint"); // 
    string path = ;
    RegistryKey rk = ;
    RegistryKey rk2 = (@"Software\Microsoft\Windows\CurrentVersion\Run");
    ("JcShutdown", path);
    ();
    ();
   }
   else //Cancel the power-on self-start   {
    ("Cancel the startup and need to modify the registry", "hint");
    RegistryKey rk = ;
    RegistryKey rk2 = (@"Software\Microsoft\Windows\CurrentVersion\Run");
    ("JcShutdown", false);
    ();
    ();
   }
  }

  private void App_Startup(object sender, StartupEventArgs e)
  {
   new AutoCleanCacheHelper().Start();
  }
 }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.