0

我正在窗口启动中创建启动条目。如果用户使用 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();
4

1 回答 1

1

条目存储在注册表中。

这是您应该添加和删除条目的方式:

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# 应用程序管理权限?清单文件

于 2013-09-11T06:08:57.210 回答