Solved

Read and write to the Registry with VB

Posted on 2014-12-11
8
131 Views
Last Modified: 2014-12-11
I am trying to write a small application using Visual Basic 2013 that will read the port number for Remote Desktop from the registry then pass that information to a text box. Then if I need to change the port number I can enter that port number in the text box and pass it back to the registry.

This bit of code works to get the number and is pretty straight forward:

 Dim readValue = My.Computer.Registry.GetValue(
        "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp", "PortNumber", Nothing)
        PortTxtBx.Text = readValue

My problem is trying to write the value back to the registry.

I know that I want build a test this application in the normal way because it has to be run elevated to administrator so testing is a little unwieldy in so far as I have to build the app then run it.

I need to keep this as simple as possible because only have to use it about 40 times.

If someone has another idea such as script I am all ears.

Thanks
0
Comment
Question by:Pat Clancy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 79

Accepted Solution

by:
arnold earned 500 total points
ID: 40493692
Your tests can be done manipulating the hkey_current_user chain.

Are you adding or modifying existing keys?
http://msdn.microsoft.com/en-us/library/aa289494(v=vs.71).aspx#vbtchaccessingregistrywithvisualbasicnetanchor5

The link includes an example with tests to determine whether you are adding a new pair, or modifying an existing one.

Always test as well as backup/record what was there before you make changes.
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40493698
HI,

pls try

  Public Sub Main
      Dim regData As Byte() = {&HF0, &HFF, &H20, &H00}
      Dim regTopKey As String = "HKEY_LOCAL_MACHINE"
      Dim regPath As String = "\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"

'      My.Computer.Registry.SetValue(regTopKey & regPath, "BinData", PortNumber, RegistryValueKind.Binary)
   End Sub

Open in new window

Regards
0
 

Author Comment

by:Pat Clancy
ID: 40493700
Yes I understand that and I can manually go in and change the numbers that need to be changed however in this day and age there should be a script that I can use to manipulate the part number.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40493781
0
 

Author Comment

by:Pat Clancy
ID: 40493853
Thank you Rgonzo1971 I appreciate your effort however, I may be missing something. I tried putting the code into a button event and I'm just getting error messages indicating that I do not have access to the registry.

I do note that "BinData" is used and the port number is actually a DWORD 32-bit and RegistryValueKind.Binary is not declared.

Thanks for your help.
0
 
LVL 79

Expert Comment

by:arnold
ID: 40493936
Are you testing with the HKEY_CURRENT_USER which allows the user to make changes without the need to elevate one's rights?

You have to take account within your app whether you have elevated rights and if not to get them.

To test your script you have to use resources to which you have access rights.  Once the functionality is established, you can change to the HKEY_LOCAL_MACHINE at which point Administrative rights will be required.
0
 

Author Comment

by:Pat Clancy
ID: 40494392
Thank you Arnold I will try changing the key to the current user to test the application.
0
 

Author Closing Comment

by:Pat Clancy
ID: 40494691
The answer helped me find a way to troubleshoot the issue. I was able gomplete my tast.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question