3

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

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

4

4 回答 4

21

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

// 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;
}
于 2011-09-30T09:12:51.487 回答
4

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

于 2011-09-30T09:23:08.843 回答
2

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

于 2011-09-30T09:11:30.423 回答
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.

于 2011-09-30T09:35:39.057 回答