Remotely enabling remote desktop with system.management problem

Please help by looking over this. I'm getting the error "System.UnauthorizedAccessException: Cannot Write to the Registry Key" I'm not sure that the path to the Terminal Server key is correct. I have admin rights and write access to the key. Thanks very much for any assistance (I'm a beginner if you can't tell..)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim servername As String

servername = txtServername.Text

Dim options As ConnectionOptions

options = New ConnectionOptions

options.Impersonation = ImpersonationLevel.Impersonate

options.Username = servername & "\administrator"

options.Password = "password"

Dim scope As ManagementScope

scope = New ManagementScope("\\" & servername & "\root\cimv2", options)

scope.Options.EnablePrivileges = True

scope.Connect()

If scope.IsConnected = True Then

MsgBox("CONNECTED")

End If

Try

Dim environmentKey As RegistryKey

environmentKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, servername).OpenSubKey("system\currentcontrolset\control\Terminal Server")

environmentKey.SetValue("fDenyTSConnections", "0")

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub



Answer this question

Remotely enabling remote desktop with system.management problem

  • Basel Alnachef

    Make sure the remote registry service is running on the remote computer


  • Onder

    Hi Ken,

    Thanks for replying. It was running but I had the sub-key path wrong and had to use int(0) as the value because it was dword. Works great now.

    Josh Horner


  • Remotely enabling remote desktop with system.management problem