VB icon

Easy Read, Write, And Delete Registry Function

Submitted on: 1/20/2015 5:20:00 PM
By: Dan Wold (from psc cd)  
Level: Intermediate
User Rating: By 6 Users
Compatibility: VB 6.0
Views: 1066
     This code allows you to Read, Write, And Delete Keys from the Registry. EASY!

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'Windows API/Global Declarations for :Easy Read, Write, And Delete Registry Function
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
' Name: Easy Read, Write, And Delete Registry Function
' Description:This code allows you to Read, Write, And Delete Keys from the Registry. EASY!
' By: Dan Wold (from psc cd)
' Side Effects:BEWARE! Editing your registry may cause harm if you dont know what your doing!

'This was created by Dan Wold (Me)
'This code is open source, feel free to use it in Anything...
'You Dont even need to Include my name.. But it would be nice
'My Email is e_man_dan@hotmail.com If you have any questions email me
' To use this code Ill show ya below ;)
' Sorry If I go into to much detail below, Just trying to make my point ;)
'Nothing special here, just reads your Windows ProductId depending on os, gave ya NT and Windows
'So dont complain ;)
'For WinNt
'Call ReadWriteDeleteRegistry("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductId", "", 1)
'For Windows
'Call ReadWriteDeleteRegistry("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ProductId", "", 1)
'Write to the Registry
'Call ReadWriteDeleteRegistry("HKEY_CURRENT_USER\Software\TestKey", "Stuff to write here", 2)
'Delete From Registry
'Call ReadWriteDeleteRegistry("HKEY_CURRENT_USER\Software\TestKey", "", 3)
'Read = 1
'Write = 2
'Delete = 3
'Reads Key
'Call ReadWriteDeleteRegistry("HKEY_CURRENT_USER\Software\TestKey", "", 1)
'Writes Key
'Call ReadWriteDeleteRegistry("HKEY_CURRENT_USER\Software\TestKey", "Stuff to write", 2)
'Deletes Key
'Call ReadWriteDeleteRegistry("HKEY_CURRENT_USER\Software\TestKey", "", 3)
'ReadWriteDelete Read = 1, Write = 2, Delete = 3
Public Sub ReadWriteDeleteRegistry(RegistryKey As String, RegistryInformation As String, ReadWriteDelete As Integer)
'Error Handling In case something goes wrong
On Error GoTo ErrHandler
'Sets the Variables to be used
Dim WSHShell, RegTemp
'Starts the Wscript Object
Set WSHShell = CreateObject("WScript.Shell")
 'Checks for Read Property (Read = 1)
 If ReadWriteDelete = 1 Then
 'Reads the specified key
 RegTemp = WSHShell.RegRead(RegistryKey)
 MsgBox RegTemp
 End If
 'Checks for Write Property (Write = 2)
 If ReadWriteDelete = 2 Then
 'Writes to the registry
 WSHShell.RegWrite RegistryKey, RegistryInformation
 MsgBox Chr(34) & RegistryKey & "\" & RegistryInformation & Chr(34) & " has been written to the registry.", vbInformation, "Success"
 End If
 'Checks for Delete Property (Delete = 3)
 If ReadWriteDelete = 3 Then
 Dim MsgDeleteKey As String
 'Makes sure you really do want to delete this key
 MsgDeleteKey = MsgBox("You are about to delete: " & RegistryKey & " From Your Registry, Do you wish to continue?", vbYesNo Or vbQuestion, "Warning!")
 End If
'Checks for which buttin the user pressed (Yes Or No)
Select Case MsgDeleteKey
 'If Yes, Delete Key
 Case vbYes
 WSHShell.RegDelete (RegistryKey)
 MsgBox RegistryKey & " Has Been Deleted!", vbInformation, "Success"
 'If No, Exit the Sub
 Case vbNo
 Exit Sub
End Select
'Error Handler Label
'Checks for specific error(s)
 'This one is for a non-existant Key
 If Err.Number = (-2147024894) Then
 MsgBox "The Registry Key (" & RegistryKey & ") doesn't exist.", vbCritical, "Error - Key Not Found"
 End If
 'This one is for an Invalid Key
 If Err.Number = (-2147024893) Then
 MsgBox "The Key (" & RegistryKey & ") is invalid.", vbCritical, "Error - Invalid Key"
 End If
End Sub

Other 2 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.