Solved

Read and write to the Registry with VB

Posted on 2014-12-11
8
129 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
8 Comments
 
LVL 77

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 50

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 77

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MsgBox 4 69
Excel Automation VBA 19 91
Automatically generate a pdf file from an excel file and email out everyday 2 62
Sub or Function is not defined 6 43
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

820 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