0

来自 https://stackoverflow.com/a/37859812/4878558的问题

我需要为启动安装的当前用户设置注册表值。由于安装进入系统模式 - 我对当前用户一无所知

还有我的代码给'System.UnauthorizedAccessException'

SecurityIdentifier sID = WindowsIdentity.GetCurrent().User;
var subKey = Registry.Users.OpenSubKey(sID + "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
subKey.SetValue("test", "test");
enter code here
4

2 回答 2

1

正如 Ripple 和我都评论的那样,不需要代码。转到设置项目中的注册表视图,右键单击 HKEY_CURRENT_USER 下的软件并添加键 Microsoft,然后是 Windows,CurrentVersion,然后是运行,添加每个键。

然后在 Run 键视图中,右键单击右侧的 Name,View 窗格并添加新的字符串值,名称就是你的名字。我假设该值是您的 exe 的路径,并且(假设它位于 Application 文件夹中)将值设为 [TARGETDIR]my.exe。

如果您的安装是“所有人”安装,那么它无法正常工作是有充分理由的。这与代码无关。在所有人安装中,自定义操作代码正在使用系统帐户(而不是安装用户)运行,因此您正在尝试为系统帐户创建运行密钥。

于 2016-06-18T16:55:05.757 回答
0

以下是编写自动启动选项的方法:

const string AutorunRegistryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run";
Registry.SetValue(AutorunRegistryKey, <AppName>, <PathToApplication>);

如果要从自动启动中删除它:

const string AutorunRelativePath = @"Software\Microsoft\Windows\CurrentVersion\Run\";
var key = Registry.CurrentUser.OpenSubKey(AutorunRelativePath, true);
    if (key != null)
    {
        key.DeleteValue(<AppName>, false);
        key.Close();
    }
于 2016-06-16T19:35:05.677 回答