C# Run application MINIMIZED at windows startup

2019-01-24 22:47发布

问题:

I got the following code to run the application at windows startup:

    private void SetStartup(string AppName, bool enable)
    {
        string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

        Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey);

        if (enable)
        {
            if (startupKey.GetValue(AppName) == null)
            {
                startupKey.Close();
                startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                startupKey.SetValue(AppName, Application.ExecutablePath.ToString());
                startupKey.Close();
            }
        }
        else
        {
            startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
            startupKey.DeleteValue(AppName, false);
            startupKey.Close();
        }
    }

It works. but I want the program to start minimized (at windows startup only). I didnt find a working code / good explanation how to do it. Can you help me please?

thanks.

回答1:

Have you tried

this.WindowState = FormWindowState.Minimized;

If you want to start minimized at windows startup only you can add extra argument to command line, like myapp.exe --start-minimized, then you can parse this parameter and detect whether you need to start minimized or not.



回答2:

Since this is only adding a registry key to SOFTWARE\Microsoft\Windows\CurrentVersion\Run which causes the OS to start the app at startup there isn't a lot you can do unless the application you want to startup accepts a command line parameter to start minimized (You could then add the parameter to the executable path of the key).

If this is a necessary function and you can't modify the program to accept a parameter to minimize the only thing I can think of doing would be to write a program that would minimize these apps after the OS has started them.



回答3:

Had a really hard time finding a good answer to this, finally found it in a really old book. On my Forms application, just opened the program.cs and changed

Application.Run(new Form1());

to

Form1 f = new Form1();
f.WindowState = FormWindowState.Minimized;
f.ShowInTaskbar = false;
Application.Run(f);

and it opens without a flicker directly to the tray. This app was more just a service, so set it to just have a notify icon and exit button when right clicked. Hope this helps!!



回答4:

Don't normally revive old threads but one Easy way including minimize to system tray, for WPF like this:

     public class EntryPoint
    {
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceManager manager = new SingleInstanceManager();
            manager.Run(args);
        }
    }

    public class SingleInstanceManager : WindowsFormsApplicationBase
    {
        SingleInstanceApplication app;

        public SingleInstanceManager()
        {
            this.IsSingleInstance = true;
        }

        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {
            app = new SingleInstanceApplication();
            app.Run();
            return false;
        }

        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            base.OnStartupNextInstance(eventArgs);
            app.Activate();
        }
    }

    public class SingleInstanceApplication : Application
    {
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            base.OnStartup(e);

            bool startMinimized = false;
            for (int i = 0; i != e.Args.Length; ++i)
            {
                if (e.Args[i] == "/StartMinimized")
                {
                    startMinimized = true;
                }
            }

            MainWindow mainWindow = new MainWindow();
            if (startMinimized)
            {
                mainWindow.WindowState = WindowState.Minimized;
            }
            mainWindow.Show();
        }


        public void Activate()
        {
            this.MainWindow.Activate();
            this.MainWindow.WindowState = WindowState.Normal;
        }
    }
}

Your Window class:

  public partial class MainWindow : Window
{
    private Window _window;

    public MainWindow()
    {
    InitializeComponent();

        SetStartup("AppName", true);
    }
 private void SetStartup(string AppName, bool enable)
    { 
        string runKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
        Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey); 
        if (enable)
        { 
            if (startupKey.GetValue(AppName) == null) 
            { 
                startupKey.Close(); 
                startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                startupKey.SetValue(AppName, Assembly.GetExecutingAssembly().Location + " /StartMinimized");
                startupKey.Close(); 
            }
        } 
        else 
        {
            startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
            startupKey.DeleteValue(AppName, false); 
            startupKey.Close(); 
        }
    }

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        if (this.WindowState == System.Windows.WindowState.Minimized)
        {
            var minimized = (_window.WindowState == WindowState.Minimized);

            _window.ShowInTaskbar = !minimized;
        }
        else
            ShowInTaskbar = true;

    }

Worked first time so had to post. I'm using WPF notifyicon, hence why i needed it to go to system tray on windows startup.



回答5:

I have strugled with the same issue, and found a working solution:

In your program.cs, handle the parameter, and then pass that parameter to Form1:

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    if (args.Length != 0){
        Application.Run(new Form1(args[0]));
    }
    else
    {
        Application.Run(new Form1("normalState"));
    }
}

In your Form1.cs, you can call a function with the passed parameter and minimize the app:

public Form1(string parameter)
{
    InitializeComponent();
    MinimizeApp(parameter);
}

For example, with this function i used, if you start the application with the -minimized parameter, then it will start minimized, a notifyicon pops up in the taskbar and a bubble saying the app is started and running in the background.

public void MinimizeApp(string parameter)
{
    if (parameter == "-minimized")
    {
        this.WindowState = FormWindowState.Minimized;
        notifyIcon1.Visible = true;
        notifyIcon1.BalloonTipText = "Program is started and running in the background...";
        notifyIcon1.ShowBalloonTip(500);
        Hide();
    }

}