[RESOLVED] Editing Registry, No Access with Admin Rights
I'm making a program that will disable forced automatic restart on update, to do this it needs to write to the HKLM registry.
When i run the program in VS2010 it returns an error "UnauthorizedAccessException", i assumed this was beacuse it was running without admin permissions however when i run the program as an administrator it doesn't make any changes to the registry or report any errors.
Code:
Imports Microsoft.Win32
Public Class Form1
Private Sub BtnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRun.Click
QuickFix()
Timer1.Enabled = True
ProgressBar1.Value = "0"
End Sub
Private Sub CheckStatus()
Dim readValue As String
readValue = My.Computer.Registry.GetValue _
("HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU", "NoAutoRebootWithLoggedOnUsers", Nothing)
If readValue = "1" Then
MsgBox("Automatic forced restart is already disabled", MsgBoxStyle.Information, "Quick Fix 001: Already Disabled.")
Else
MsgBox("Automatic forced restart is enabled", MsgBoxStyle.Information, "Quick Fix 001: Already Disabled.")
End If
End Sub
Private Sub QuickFix()
''My.Computer.Registry.LocalMachine.CreateSubKey("SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU")
My.Computer.Registry.LocalMachine.SetValue("SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU", _
"NoAutoRebootWithLoggedOnUsers", "1")
My.Computer.Registry.LocalMachine.Close()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Value = ProgressBar1.Value + 1
If ProgressBar1.Value = 50 Then
CheckStatus()
End If
If ProgressBar1.Value = 100 Then
Timer1.Enabled = False
End If
End Sub
Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
End
End Sub
End Class
Re: Editing Registry, No Access with Admin Rights
By default, you can only read from the registry. Writing to the registry requires you pass a boolean as an argument when writing.
Also, never, ever use End unless you absolutely have to. Instead, use Me.Close. And, MsgBox is an outdated class carried over from VB6. Instead, use MessageBox.Show with the appropriate overloads.
Finally, your CheckStatus method should not be a sub. Instead, make it a function. Methods and functions as essentially the same, except that a function can return a value. So instead of running a sub and throwing up a MessageBox within it, you should make it a function (as a boolean) and call the messagebox from the value returned from the function.
Re: Editing Registry, No Access with Admin Rights
Thanks for your help, and the info on the MsgBox, i've only ever used VB6 and that was a few years back, this is my first time using VB.net.