Search Tools Links Login

Easy Read, Write, And Delete Registry Function


This code allows you to Read, Write, And Delete Keys from the Registry. EASY!

Original Author: Dan Wold

Side Effects

BEWARE! Editing your registry may cause harm if you dont know what your doing!

API Declarations

None!

Code

'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_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionProductId", "", 1)
'For Windows
'Call ReadWriteDeleteRegistry("HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionProductId", "", 1)
'*********************************************************************
'********************************************************************
'Write to the Registry
'Call ReadWriteDeleteRegistry("HKEY_CURRENT_USERSoftwareTestKey", "Stuff to write here", 2)
'********************************************************************
'********************************************************************
'Delete From Registry
'Call ReadWriteDeleteRegistry("HKEY_CURRENT_USERSoftwareTestKey", "", 3)
'********************************************************************
'********************************************************************
'Read = 1
'Write = 2
'Delete = 3
'Reads Key
'Call ReadWriteDeleteRegistry("HKEY_CURRENT_USERSoftwareTestKey", "", 1)
'Writes Key
'Call ReadWriteDeleteRegistry("HKEY_CURRENT_USERSoftwareTestKey", "Stuff to write", 2)
'Deletes Key
'Call ReadWriteDeleteRegistry("HKEY_CURRENT_USERSoftwareTestKey", "", 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"
Err

'If No, Exit the Sub
Case vbNo
Exit Sub
End Select
'Error Handler Label
ErrHandler:
'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

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 93 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.