我们如何从 msconfig 窗口中删除启动条目
本文关键字:删除 启动 窗口 msconfig 我们 | 更新日期: 2023-09-27 18:30:56
我正在窗口启动中创建启动条目。如果用户使用 msconfig 启动窗口取消选择条目,我的应用将创建一个重复的条目。我需要删除现有条目(如果它存在)或跳过创建重复项。我该怎么做?
我创建启动条目的代码是这样的:-
string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "''" + "MyexeName.exe";
if (System.IO.File.Exists(startUpFolderPath))
{
return;
}
WshShellClass wshShell = new WshShellClass();
IWshRuntimeLibrary.IWshShortcut shortcut;
shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(startUpFolderPath);
shortcut.TargetPath = Application.ExecutablePath;
shortcut.WorkingDirectory = Application.StartupPath;
shortcut.Save();
这些条目存储在注册表中。
这是您应该如何添加和删除条目:
using Microsoft.Win32;
private void SetStartup()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE''Microsoft''Windows''CurrentVersion''Run", true);
if (ShouldAdd)
rk.SetValue(AppName, Application.ExecutablePath.ToString());
else
rk.DeleteValue(AppName, false);
}
以下是不同条目的列表:
https://stackoverflow.com/a/5394144/2027232
要获得管理员权限,您需要向应用添加清单文件:
按 Ctrl+Shift+A(添加新项),然后选择(应用程序清单文件)
打开清单文件并更改行:<requestedExecutionLevel level="asInvoker" uiAccess="false" />
自
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
,然后按保存。
详细信息:如何授予 C# 应用管理权限? 清单文件