【问题标题】:Autorun the Application using C# [closed]使用 C# 自动运行应用程序 [关闭]
【发布时间】:2011-09-30 09:09:43
【问题描述】:

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

谁能帮助我如何在 C# 上做到这一点。

【问题讨论】:

  • 我想你可能正在寻找这个http://stackoverflow.com/questions/234231/creating-application-shortcut-in-a-directory。您可以第一次手动运行该应用程序,让我们说 OnClose 通过为其提供 Windows 启动路径来运行链接中的 sn-p。
  • @Ragzitsu 我真的不认为这就是他想要的......

标签: c# autorun


【解决方案1】:

这是您将应用添加到启动的方式:

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

并删除它:

// 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);

还有我代码中的 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;
}

【讨论】:

  • 这在安装程序中做得更好,因为它需要管理员权限。
  • @David Heffernan 我认为不会,因为您正在为当前用户添加它。我目前在我的应用程序中使用它,它可以在不提示提升权限的情况下工作(在 Windows 7 中)。
  • 抱歉,没看到你是为 HKCU 做的。但是,通常希望为所有用户添加自动运行,在这种情况下我的评论是有效的。
  • @DavidHeffernan 你说得有道理,虽然我真的很喜欢 Chrome 的方法,即按用户安装应用程序(在 Local 文件夹中),尤其是对于多用户的小型应用程序用户环境。因此,我的方法在这里。
  • Chrome 的方法在多用户环境中是一场灾难。但他们这样做是为了通过后门潜入公司。
【解决方案2】:

为您的应用程序创建一个 Windows 服务,注册该 Windows 服务并将启动属性设置为自动。 现在您的服务将在 Windows 启动时自动启动 和 看到这个链接: http://www.geekpedia.com/tutorial151_Run-the-application-at-Windows-startup.html

【讨论】:

  • 在我看来,这是一种可怕的做法。只有真正应该作为服务运行的服务才应该使用这种方法。具有 UI 的应用还有其他方法可以做到这一点!
【解决方案3】:

程序实现这一点的大多数方式是通过安装程序,它可以做很多事情,包括修改注册表以确保程序在启动时启动,但是您应该始终让您的用户选择禁用此行为。

【讨论】:

    【解决方案4】:

    我认为比在注册表中添加一个键更好的方法是在 Windows StartUp 文件夹中添加一个快捷方式:它对用户更透明,您可以让用户选择删除快捷方式,如果他不希望您的应用程序在 Windows 启动时启动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      相关资源
      最近更新 更多