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
VB AUTO LOG APP
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?
Visual Basic.NETVB ScriptWindows 7

Avatar of undefined
Last Comment
Tom Powers

8/22/2022 - Mon
Peter Hutchison

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.
ASKER
Tom Powers

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
McKnife

No matter what method you use, it can only succeed after elevating  Did you elevate before executing these scripts?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
Tom Powers

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?
McKnife

"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.
ASKER
Tom Powers

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
McKnife

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Tom Powers

Thanks I know the workaround I'm going to use.