Link to home
Start Free TrialLog in
Avatar of Tom Powers
Tom Powers

asked on

I Can write to Registry Local Machine using VB SCript but in VB.NET write to same key I get System.UnauthorizedAccessException

I Can write to Registry Local Machine using VB SCript but in VB.NET write to same key I get System.UnauthorizedAccessException. Is there some way to code this VB.NET Gui I'm writing to write static values to HK\Local Machine instead of vbscript or Merge Reg Files.
here is gui
User generated image
vb.net code
Imports Microsoft.Win32
Public Class Form1
    Public Sub RegValues()
        Dim oReg As RegistryKey
        oReg = Registry.LocalMachine.OpenSubKey("Software", True)
        oReg = oReg.OpenSubKey("Microsoft\Windows NT\CurrentVersion\Winlogon")

        oReg.SetValue("AutoAdminLogon", 1)
        oReg.Close()
    End Sub

Open in new window


vbscript
Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."
 
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
strValueName = "AutoAdminLogon"
strValue = 1

objRegistry.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue

Open in new window


I could write to Registry Local Machine in XP With VB6 ? aNY CLEVER CODE?
Avatar of Peter Hutchison
Peter Hutchison
Flag of United Kingdom of Great Britain and Northern Ireland image

I wouldn't use scripts to update registry,  instead I would use Group Policy to apply registry changes.
Computer Configuration, Policies, Windows Settings, Security Settings, Registry., Add Key and add the entry you require.
Avatar of Tom Powers
Tom Powers

ASKER

Pete the problem is that we have 8 different auto login accounts and each have different permissions and pertain to 8 different sections or Departments at the Hospital I work at so I just wanted to make a GUI that set AutoAdminlogin key Default username and password. I just don't understand why VBScript works but a .NET APP doesn't have Write access to LocalMachine/SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
No matter what method you use, it can only succeed after elevating  Did you elevate before executing these scripts?
Yes I used Runas Different User with admin rights on all desktops. I'm reading online about
Public Enumeration RegistryPermissionAccess but I have not clue what to code? Anybody got some magic code?
"Runas Different User with admin rights" is not the same as elevating. When you logon (or runas) a different user with admin rights, you still need to elevate. So unless you didn't see a UAC prompt at some point, you haven yet elevated and write to HKLM will fail.
I just run the vb script with admin rights my Username. So it's bizarre McKnife UAC is was set to never notify so I raised it to the top for notification but UAC never notifies me. So No Coders I guess?
ASKER CERTIFIED SOLUTION
Avatar of McKnife
McKnife
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks I know the workaround I'm going to use.