We help IT Professionals succeed at work.

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

408 Views
Last Modified: 2016-03-11
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?
Comment
Watch Question

Peter HutchisonSenior Network Systems Specialist
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
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
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
No matter what method you use, it can only succeed after elevating  Did you elevate before executing these scripts?

Author

Commented:
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?
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
"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.

Author

Commented:
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?
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.