Search code examples
c#windowsstartuplaunchsystem-tray

How do I hide a C# program at Windows startup, but not when opening the program normally?


I have a (.NET) program that boots automatically when Windows starts up. However, I don't want the main GUI window to show (I would have it hidden away in the tray).

However, I want the window to of course show when the user opens the program in the normal way from the taskbar/quicklaunch.

What code can I use to solve this problem?

To add the registry key I use to load the program on Windows reboot, here is the code:

RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

rk.SetValue(softwareName, Application.ExecutablePath.ToString());

Solution

  • Just add a command line argument in the Startup shortcut or the registry's Run key, like /hidewindow.

    Then in main(string[] args) check if args.Any(a => a == "/hidewindow") and don't show your window.