4

I want to create an application that's automatically run after booting the machine.

Can anyone help me on how can I do it on C#.

2
  • I think you may be looking for this http://stackoverflow.com/questions/234231/creating-application-shortcut-in-a-directory. You can run the application manually first time and lets say OnClose run the snippet in the link by giving it windows startup path.
    – Raghu
    Commented Sep 30, 2011 at 9:20
  • @Ragzitsu I don't really think that's what he wants...
    – alex
    Commented Sep 30, 2011 at 9:23

4 Answers 4

23

This is how you add an app to startup:

// The path to the key where Windows looks for startup applications
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

if (!IsStartupItem())
    // Add the value in the registry so that the application runs at startup
    rkApp.SetValue("My app's name", Application.ExecutablePath.ToString());

And to remove it:

// The path to the key where Windows looks for startup applications
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

if(IsStartupItem())
    // Remove the value from the registry so that the application doesn't start
    rkApp.DeleteValue("My app's name", false);

And the IsStartupItem function in my code:

private bool IsStartupItem()
{
    // The path to the key where Windows looks for startup applications
    RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

    if (rkApp.GetValue("My app's name") == null)
        // The value doesn't exist, the application is not set to run at startup
        return false;
    else
        // The value exists, the application is set to run at startup
        return true;
}
6
  • 1
    This is much better done in an installer since it requires admin rights. Commented Sep 30, 2011 at 9:14
  • 2
    @David Heffernan I don't think it does, since you're adding it for the current user. I currently use it in my app and it works without prompting for elevated rights (in Windows 7).
    – alex
    Commented Sep 30, 2011 at 9:16
  • Sorry, didn't see that you were doing it for HKCU. However, it's common to want to add autorun for all users, in which case my comment stands. Commented Sep 30, 2011 at 9:20
  • @DavidHeffernan you have a point, although I really like Chrome's approach, where the app is installed per user (in the Local folder), especially for small apps in multi-user environments. Thus, my approach here.
    – alex
    Commented Sep 30, 2011 at 9:22
  • 2
    Chrome's approach is a disaster in multi-user environment. But they did it to sneak into corporations by the back door. Commented Sep 30, 2011 at 9:27
4

Create a windows service for your application register that windows service and set startup property as automatic . Now your service will start automatically when ever windows starts and see this link: http://www.geekpedia.com/tutorial151_Run-the-application-at-Windows-startup.html

1
  • 5
    In my opinion, that's a horrible way to do it. Only real services, that actually should run as services, should use this approach. Apps that have a UI have other ways of doing this!
    – alex
    Commented Sep 30, 2011 at 9:31
2

Most of the ways programs achieve this is through an installer, which can do many things including modifying the registry to ensure their program is started upon startup, however you should always give your users the option to disable this behaviour.

2

I think a better way rather than adding a key in the registry is too add a shortcut in the Windows StartUp folder: it is more transparent to the user and you let the choice to the user to delete the shortcut if he doesn't want your application to start at Windows boot.

Not the answer you're looking for? Browse other questions tagged or ask your own question.