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

发布于 2024-12-07 12:19:46 字数 61 浏览 0 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只有影子陪我不离不弃 2024-12-14 12:19:46

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

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

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;
}
久伴你 2024-12-14 12:19:46

为您的应用程序创建一个Windows服务,注册该Windows服务并将启动属性设置为自动。
现在,当 Windows 启动时,您的服务将自动启动

请参阅此链接:
http://www.geekpedia.com/tutorial151_Run-the-application -at-Windows-startup.html

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

秋日私语 2024-12-14 12:19:46

程序实现此目的的大多数方式是通过安装程序,它可以执行许多操作,包括修改注册表以确保其程序在启动时启动,但是您应该始终为用户提供禁用此行为的选项。

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.

耳根太软 2024-12-14 12:19:46

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文