使用C#自动运行应用程序

Bry*_*yan 2 c# autorun

我想创建一个在启动机器后自动运行的应用程序.

任何人都可以帮助我如何在C#上做到这一点.

ale*_*lex 20

这是您向启动添加应用程序的方式:

// 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());
Run Code Online (Sandbox Code Playgroud)

并删除它:

// 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);
Run Code Online (Sandbox Code Playgroud)

我的代码中的IsStartupItem函数:

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;
}
Run Code Online (Sandbox Code Playgroud)

  • @David Heffernan我不认为它,因为你是为当前用户添加它.我目前在我的应用程序中使用它,它可以在不提示提升权限的情况下工作(在Windows 7中). (2认同)
  • Chrome的方法在多用户环境中是一场灾难.但他们这样做是为了通过后门潜入公司. (2认同)