<分区>
我想创建一个在机器启动后自动运行的应用程序。
任何人都可以帮助我如何在 C# 上做到这一点。
<分区>
我想创建一个在机器启动后自动运行的应用程序。
任何人都可以帮助我如何在 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/