Autorun the Application using C# [closed]

╄→尐↘猪︶ㄣ 提交于 2019-11-30 16:43:13

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!