Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Read and write to the Registry with VB

Posted on 2014-12-11
8
Medium Priority
?
135 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Suggested Courses

721 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