做了几个增删改注册表项的函数,需要时可以拷贝来用:
Imports Microsoft.Win32 ' provided by mscorlib.dll
Public Class RegistryAccess
Public Shared Function GetRegistryKey(ByVal registryRoot As RegistryKey, ByVal keyName As String, ByVal valueName As String) As Object
'
' registryKey = Microsoft.Win32.Registry.LocalMachine | .ClassesRoot | .CurrentConfig | .CurrentUser | .DynData | .PerformanceData | .Users
' keyName = "Software//Microsoft//IE Setup/Setup"
' valueName = "Path"
'
Dim subKey As RegistryKey
subKey = registryRoot.OpenSubKey(keyName, False)
Dim valueData As Object
If (Not subKey Is Nothing) Then
valueData = subKey.GetValue(valueName)
subKey.Close()
End If
Return valueData
End Function
Public Shared Function SetRegistryKey(ByVal registryRoot As RegistryKey, ByVal keyName As String, ByVal valueName As String, ByVal valueData As Object) As Boolean
'
Dim subKey As RegistryKey
subKey = registryRoot.OpenSubKey(keyName, True)
If subKey Is Nothing Then
' Create it if it doesn't exist
subKey = registryRoot.CreateSubKey(keyName)
End If
If (Not subKey Is Nothing) Then
subKey.SetValue(valueName, valueData)
subKey.Close()
Else
' Failure
Return False
End If
Return True
End Function
Public Shared Function DeleteRegistryKey(ByVal registryRoot As RegistryKey, ByVal keyName As String, Optional ByVal ErrorOnFail As Boolean = False) As Boolean
'
Try
registryRoot.DeleteSubKeyTree(keyName)
Return True
Catch ex As Exception
If ErrorOnFail Then
Throw ex
Else
Return False
End If
End Try
End Function
End Class
------------------
调用方法示例:
RegistryAccess.SetRegistryKey(Microsoft.Win32.Registry.LocalMachine, "Software//Company Name//Section Name//Key Name", "ValueName", "ValueData")