VB Script - Read and Write to Windows Registry

VB Script - Read and Write to Windows Registry

The attached project will check for the existence of Windows Registry key and then read / write to this Registry Key.  The example will prompt user for a beginning serial number then increment the value automatically each time the program is run.

Scripts

Check if Registry Key Exists then create if not:

CODE: 

'Resume Script if Error Occurs'Resume Script if Error Occurs
On Error Resume Next

' Create a WSH Shell object:
Set wshShell = CreateObject( "WScript.Shell" )

' Create a new key:
wshShell.RegWrite "HKCU\TestKey\", ""

' Read the values if already exists:
CurrentState = (wshShell.RegRead( "HKCU\TestKey\StringTestValue" ))

'Optional Error Handling
If Err.Number <> 0 Then
MsgBox("Error Occured - Variable did not exist")
End If

'Prompt for String - Use Previous Value if Available
MyString = Cint(InputBox("Please Serial Number", "Enter Value", CurrentState))

' Write string value:
wshShell.RegWrite "HKCU\TestKey\StringTestValue", MyString, "REG_SZ"

' Release the object
Set wshShell = Nothing

Read, Prompt User, then Write to Registry:

CODE: 

' Create a WSH Shell object:
Set wshShell = CreateObject( "WScript.Shell" )

' Read the value from Registry:
MyString = (wshShell.RegRead( "HKCU\TestKey\StringTestValue" ))

'MsgBox(MyString)

MySerial = MyString
MyString = MyString + 1

'Write string value:
wshShell.RegWrite "HKCU\TestKey\StringTestValue", MyString, "REG_SZ"

' Release the object
Set wshShell = Nothing