对注册表的操作:简单的增删改查
Imports Microsoft.Win32
Public Class Form1
Private RegCurrentConfig As RegistryHive = "-2147483643"
Private RegClassUsers As RegistryHive = "-2147483645"
Private RegLocalMachine As RegistryHive = "-2147483646"
Private RegCurrentUser As RegistryHive = "-2147483647"
Private RegClassRoot As RegistryHive = "-2147483648"
''' <summary>
''' 操作注册表
''' </summary>
''' <param name="Hive"></param>
''' <param name="Key"></param>
''' <param name="ValueName"></param>
''' <param name="ErroInfo"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function RegValue(ByVal Hive As RegistryHive, ByVal Key As String, ByVal ValueName As String, Optional ByRef ErroInfo As String = "") As String
RegValue = ""
Dim objParentKey As RegistryKey
Dim objSubKey As RegistryKey
Select Case Hive
Case RegistryHive.ClassesRoot
objParentKey = Registry.ClassesRoot
Case RegistryHive.CurrentConfig
objParentKey = Registry.CurrentConfig
Case RegistryHive.CurrentUser
objParentKey = Registry.CurrentUser
Case RegistryHive.LocalMachine
objParentKey = Registry.PerformanceData
Case RegistryHive.Users
objParentKey = Registry.Users
End Select
'Create subkey
Dim rk As RegistryKey
rk = objParentKey.OpenSubKey("Hello", True)
rk.CreateSubKey("MyRegistrySubKey", True)
'Set value
rk.SetValue("Hello", "Kitty")
'get Value
Try
objSubKey = objParentKey.OpenSubKey(Key)
If IsNothing(objSubKey) = False Then
RegValue = (objSubKey.GetValue(ValueName))
End If
Catch ex As Exception
ErroInfo = ex.Message
Finally
If ErroInfo = "" And RegValue = "" Then
ErroInfo = "No value"
End If
End Try
'delete subkey
rk.DeleteSubKey("xxx", False)
rk.Close()
Return RegValue
End Function
'test
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox(RegValue(RegCurrentUser, "Control Panel\Colors", "ActiveBorder"))
End Sub
End Class
更加详细的信息,可以查询:http://www.lob.cn/Special/registry/Index.shtml