Public Class RegisterOP
'---------------------------------------------------------
'2008-1-10 TNT
'Imports Microsoft.Win32
'功能:
' 实现了对注册表的几乎所有操作
'注:
'输入的路径用\分开,例如“HKEY_LOCAL_MACHINE\SOFTWARE\Wom”
'---------------------------------------------------------
#Region "属性"
''' <summary>
''' 返回所以子顶的名称
''' </summary>
''' <param name="position">指定项的位置</param>
''' <value>以数组的形式返回所以子项的名称</value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property AllChildItemName(ByVal position As String) As String()
Get
Dim rootitem As RegistryKey = OpenItem(position)
Try
Return rootitem.GetSubKeyNames
Catch e As Exception
Throw e
End Try
End Get
End Property
''' <summary>
''' 返回所以键的名称
''' </summary>
''' <param name="position">键所在的项的位置</param>
''' <value>以数组的形式返回所以键的名称</value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property AllChildValue(ByVal position As String) As String()
Get
Dim rootitem As RegistryKey = OpenItem(position)
Try
Return rootitem.GetValueNames
Catch e As Exception
Throw e
End Try
End Get
End Property
''' <summary>
''' 返回子项的数目
''' </summary>
''' <param name="position">指定项的位置</param>
''' <value></value>
''' <returns>返回子项的数目</returns>
''' <remarks></remarks>
Public ReadOnly Property ChildItemCount(ByVal position As String) As Integer
Get
Dim rootitem As RegistryKey = OpenItem(position)
Try
Return rootitem.SubKeyCount
Catch e As Exception
Throw e
End Try
End Get
End Property
''' <summary>
''' 返回指定键的值
''' </summary>
''' <param name="position">指定键的位置</param>
''' <param name="keyname">指定键的名称</param>
''' <value></value>
''' <returns>返回指定键的值</returns>
''' <remarks></remarks>
Public ReadOnly Property KeyValue(ByVal position As String, ByVal keyname As String) As String
Get
Dim rootitem As RegistryKey = OpenItem(position)
Try
Return rootitem.GetValue(keyname)
Catch e As Exception
Throw e
End Try
End Get
End Property
''' <summary>
''' 返回指定项的已经被赋值的键的个数
''' </summary>
''' <param name="position"></param>
''' <value></value>
''' <returns></returns>
''' <remarks>作用是查看有多少个键用的是系统默认值</remarks>
Public ReadOnly Property KeyCount(ByVal position As String) As Integer
Get
Dim rootitem As RegistryKey = OpenItem(position)
Try
Return rootitem.ValueCount
Catch e As Exception
Throw e
End Try
End Get
End Property
#End Region
#Region "方法"
#Region "定位子项"
'定位子项
Private Function OpenItem(ByVal name As String) As RegistryKey
Dim strname() As String = name.Split("\")
Dim destitem As RegistryKey
Dim tempitem As RegistryKey
Select Case strname(0).ToUpper
Case "HKEY_CLASSES_ROOT"
tempitem = Registry.ClassesRoot
Case "HKEY_CURRENT_USER"
tempitem = Registry.CurrentUser
Case "HKEY_LOCAL_MACHINE"
tempitem = Registry.LocalMachine
Case "HKEY_USERS"
tempitem = Registry.Users
Case "HKEY_CURRENT_CONFIG"
tempitem = Registry.CurrentConfig
Case "HKEY_DYN_DATA"
tempitem = Registry.DynData
Case "HKEY_PERFORMANCE_DATA"
tempitem = Registry.PerformanceData
Case Else
tempitem = Nothing
End Select
If strname.Length > 1 Then
For i As Integer = 1 To strname.Length - 1
tempitem = GetRootItem(strname(i), tempitem)
Next
End If
If tempitem IsNot Nothing Then
destitem = tempitem
Return destitem
Else
Return Nothing
End If
End Function
Private Function GetRootItem(ByVal name As String, ByVal key As RegistryKey) As RegistryKey
'OpenSubKey(name, True)的意思是打开并修改项,如果只是读取则用false,等会将用到
Dim tempitem As RegistryKey = key.OpenSubKey(name, True)
Return tempitem
End Function
#End Region
''' <summary>
''' 添加新项
''' </summary>
''' <param name="Position">新项的位置</param>
''' <param name="itemname">新项的名称</param>
''' <param name="keyname">键的名称</param>
''' <param name="keyvalue">键值</param>
''' <remarks>添加新项的同时添加一个键</remarks>
Public Sub CreatItem(ByVal Position As String, ByVal itemname As String, ByVal keyname As String, Optional ByVal keyvalue As String = "")
Try
Dim rootitem As RegistryKey = OpenItem(Position)
Dim destitem As RegistryKey = rootitem.CreateSubKey(itemname)
destitem.SetValue(keyname, keyvalue)
Catch e As Exception
Throw e
End Try
End Sub
''' <summary>
''' 添加新项
''' </summary>
''' <param name="Position">新项的位置</param>
''' <param name="itemname">新项的名称</param>
''' <remarks></remarks>
Public Sub CreatItem(ByVal Position As String, ByVal itemname As String)
Try
Dim rootitem As RegistryKey = OpenItem(Position)
Dim destitem As RegistryKey = rootitem.CreateSubKey(itemname)
'destitem.SetValue(keyname, keyvalue)
Catch e As Exception
Throw e
End Try
End Sub
''' <summary>
''' 为指定的项添加键
''' </summary>
''' <param name="position">指定的项的位置</param>
''' <param name="keyname">键的名称</param>
''' <param name="keyvalue">键值</param>
''' <remarks></remarks>
Public Sub AddKey(ByVal position As String, ByVal keyname As String, ByVal keyvalue As String)
Dim rootitem As RegistryKey = OpenItem(position)
rootitem.SetValue(keyname, keyvalue)
End Sub
''' <summary>
''' 删除指定项
''' </summary>
''' <param name="position">指定的项的位置</param>
''' <param name="itemname">指定的项的名称</param>
''' <remarks></remarks>
Public Sub DelItem(ByVal position As String, ByVal itemname As String)
Dim rootitem As RegistryKey = OpenItem(position)
'DeleteSubKey(itemname, True) 加上True更安全,因为加上后如果要删除的项不存在就不会报错
Try
rootitem.DeleteSubKey(itemname, True)
Catch e As Exception
Throw e
End Try
End Sub
''' <summary>
''' 删除指定的子项树
''' </summary>
''' <param name="position">指定的项的位置</param>
''' <param name="itemname">指定的项的名称</param>
''' <remarks></remarks>
Public Sub DelItemTree(ByVal position As String, ByVal itemname As String)
Dim rootitem As RegistryKey = OpenItem(position)
Try
rootitem.DeleteSubKeyTree(itemname)
Catch e As Exception
Throw e
End Try
End Sub
''' <summary>
''' 删除键
''' </summary>
''' <param name="position">指定的键的位置</param>
''' <param name="keyname">指定的键的名称</param>
''' <remarks></remarks>
Public Sub DelKey(ByVal position As String, ByVal keyname As String)
Dim rootitem As RegistryKey = OpenItem(position)
Try
rootitem.DeleteValue(keyname)
Catch e As Exception
Throw e
End Try
End Sub
''' <summary>
''' 修改銉值
''' </summary>
''' <param name="position">指定的键的位置</param>
''' <param name="keyname">指定的键的名称</param>
''' <param name="newvalue">改变后的值</param>
''' <remarks></remarks>
Public Sub ModifyKey(ByVal position As String, ByVal keyname As String, ByVal newvalue As String)
Dim rootitem As RegistryKey = OpenItem(position)
Try
rootitem.SetValue(keyname, newvalue)
Catch e As Exception
Throw e
End Try
End Sub
#End Region
End Class