Solved

Read and write to the Registry with VB

Posted on 2014-12-11
8
130 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 78

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 51

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 78

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

738 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