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

标签 c# autorun

<分区>

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

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

最佳答案

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

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

关于c# - 使用 C# 自动运行应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7608225/

相关文章:

c# - C# 中的 myArray.GetValue(2) 和 myArray[2] 有什么区别?

c# - 为什么 Assembly.GetType() 找不到我的类(class)?

visual-studio-2008 - setup.exe 和 autorun.inf

android - 如何在手机开机时自动运行应用程序

c# - 使用 .net 平台编写的应用程序自动运行 CD

c# - 如何在单击按钮时检查 gridview 列中复选框的状态

c# - 有效地将分隔字符连接成单词

c# - 如何覆盖 &&(逻辑和运算符)?

autorun - 为什么关机时无法将文件上传到保管箱?

.net - 如何在 Windows 8 中自动运行需要管理员权限的应用程序