Question

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#.

Était-ce utile?

La solution

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

Autres conseils

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

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.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top