3

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.

Ron
  • 3,975
  • 17
  • 80
  • 130
  • I had found this question today, in January 2019 and the subroutine presented by Ron still works today, with a slight modification: startupKey=Registry.CurrentUser.OpenSubKey(runKey) Thank you for your subroutine. Short and simple. – Bogdan Doicin Jan 25 '19 at 13:46

5 Answers5

14

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.

DReJ
  • 1,966
  • 15
  • 14
  • You could also create a link file (*.lnk) with the option to start minimized set, but I would prefer this answer. – VVS Dec 15 '10 at 19:41
  • 1
    @Ron: Check this out: http://msdn.microsoft.com/en-us/library/96s74eb0(v=vs.80).aspx – DReJ Dec 15 '10 at 20:10
  • 1
    In the static main method the parameters show up as an array of strings passed in. Here is the documentation on it in MSDN http://msdn.microsoft.com/en-us/library/aa288457(v=vs.71).aspx – Tyler Thalman Dec 15 '10 at 20:10
  • Alright. I added startupKey.SetValue(AppName, Application.ExecutablePath.ToString() + " /minimized"); and in the program.cs added string[] args to the main function. it worked. but something else - wierd happedend. my program read an xml file on load. it works if I run the program manually, but when I run it on startup, it doesnt read the xml file. any ideas why? – Ron Dec 15 '10 at 20:29
  • 1
    @Ron: Maybe the problem is in working directory. If your app looks for xml in the current directory it may not find it. Try to check current directory. – DReJ Dec 15 '10 at 20:35
  • @DReJ: running the program from registry make the current directory system32/something so yeah, that was the problem. thank you. – Ron Dec 15 '10 at 20:44
1

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.

1

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.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Kevin
  • 11
  • 1
0

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!!

Dave_750
  • 1,225
  • 1
  • 13
  • 28
  • 2
    the question was to start the app with Window Startup with minimized window ..what you have answered is how to run app with Window minimized.. And also `f.ShowInTaskbar = false;` will make your program launch non-identifyable, you will never be able to see the UI, if I am not wrong.. – techBeginner Sep 13 '12 at 09:27
0

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();
    }

}
slavoo
  • 5,798
  • 64
  • 37
  • 39
dodo
  • 459
  • 2
  • 8
  • 21