【发布时间】:2014-05-06 15:39:58
【问题描述】:
当我尝试设置/打开注册表项时出现异常:
Requested registry access is not allowed.
我可以将requestedExecutionLevel 键设置为requireAdministrator,但我不希望每次在应用程序启动时看到管理员提示。并且有些用户没有管理员权限。按需请求管理员权限是完美的。
我已经尝试过的代码:
Dim regStartUp As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
Dim value As String
value = regStartUp.GetValue("App")
If value <> Application.ExecutablePath.ToString() & " startup" Then
regStartUp.CreateSubKey("App")
regStartUp.SetValue("App", Application.ExecutablePath.ToString() & " startup")
End If
Dim CommandLineArguments As String() = Environment.GetCommandLineArgs()
Dim i As Integer
Dim hideme As Boolean = False
For i = 0 To CommandLineArguments.GetUpperBound(0)
Console.WriteLine(CommandLineArguments(i) & vbCrLf)
If CommandLineArguments(i).ToLower() = "startup" Then
hideme = True
End If
Next
If hideme Then
Me.Hide()
End If
【问题讨论】:
-
您正在对用户的机器进行大量的更改。这需要通过 UAC 提示告诉用户它。隐藏它极大地损害了用户保持机器稳定的愿望。而且它不起作用,这是强制执行的。
标签: .net vb.net uac elevated-privileges