1 ''' <summary> 2 ''' 注册表设置值 3 ''' </summary> 4 ''' <param name="strKey"></param> 5 ''' <param name="strName"></param> 6 ''' <param name="strValue"></param> 7 ''' <returns></returns> 8 ''' <remarks></remarks> 9 Private Function SetRegistry(ByVal strKey As String, ByVal strName As String, ByVal strValue As String) As Boolean 10 Try 11 Dim Key_LocalMachine As Microsoft.Win32.RegistryKey 12 Key_LocalMachine = My.Computer.Registry.LocalMachine 'get the key path of HKEY_LOCAL_MACHINE 13 Dim Key_Created As Microsoft.Win32.RegistryKey 14 Key_Created = Key_LocalMachine.OpenSubKey(strKey, True) 'set the true for can write 15 If Key_Created Is Nothing Then 16 Key_Created = Key_LocalMachine.CreateSubKey(strKey) 'careat the key 17 End If 18 Key_Created.SetValue(strName, strValue) 'set the values of the key 19 Return True 20 Catch ex As Exception 21 Return False 22 End Try 23 End Function 24 ''' <summary> 25 ''' 获取注册表值 26 ''' </summary> 27 ''' <param name="strKey"></param> 28 ''' <param name="strName"></param> 29 ''' <param name="strValue"></param> 30 ''' <returns></returns> 31 ''' <remarks></remarks> 32 Private Function GetValueFromRegistry(ByVal strKey As String, ByVal strName As String, ByRef strValue As String) As Boolean 33 Try 34 Dim Key_LocalMachine As Microsoft.Win32.RegistryKey 35 Key_LocalMachine = My.Computer.Registry.LocalMachine 'get the key path of HKEY_LOCAL_MACHINE 36 Dim Key_Get As Microsoft.Win32.RegistryKey 37 Key_Get = Key_LocalMachine.OpenSubKey(strKey) 38 If Key_Get Is Nothing Then 39 Return False 40 End If 41 strValue = Key_Get.GetValue(strName) 'get the values of the key 42 Return True 43 Catch ex As Exception 44 Return False 45 End Try 46 End Function 47 ''' <summary> 48 ''' 判断值是否存在 49 ''' </summary> 50 ''' <param name="strKey"></param> 51 ''' <param name="strName"></param> 52 ''' <returns></returns> 53 ''' <remarks></remarks> 54 Private Function RegistryExist(ByVal strKey As String, ByVal strName As String) As Boolean 55 Try 56 Dim Key_LocalMachine As Microsoft.Win32.RegistryKey 57 Key_LocalMachine = My.Computer.Registry.LocalMachine 'get the key path of HKEY_LOCAL_MACHINE 58 Dim Key_Exist As Microsoft.Win32.RegistryKey 59 Key_Exist = Key_LocalMachine.OpenSubKey(strKey, True) 'set the true for can write 60 If Key_Exist Is Nothing Then 61 Return False 62 End If 63 If Key_Exist.GetValue(strName) Is Nothing Then 64 Return False 65 End If 66 Return True 67 Catch ex As Exception 68 Return False 69 End Try 70 End Function 71 ''' <summary> 72 ''' 删除注册表对应值 73 ''' </summary> 74 ''' <param name="strKey"></param> 75 ''' <param name="strName"></param> 76 ''' <returns></returns> 77 ''' <remarks></remarks> 78 Private Function DeleteRegistry(ByVal strKey As String, ByVal strName As String) As Boolean 79 Try 80 Dim Key_LocalMachine As Microsoft.Win32.RegistryKey 81 Key_LocalMachine = My.Computer.Registry.LocalMachine 'get the key path of HKEY_LOCAL_MACHINE 82 Dim Key_Del As Microsoft.Win32.RegistryKey 83 Key_Del = Key_LocalMachine.OpenSubKey(strKey, True) 'set the true for can write 84 If Key_Del Is Nothing Then 85 Return False 86 End If 87 Key_Del.DeleteValue(strName) 'delete the values of the key'name 88 Return True 89 Catch ex As Exception 90 Return False 91 End Try 92 End Function
调用:
1 SetRegistry("SOFTWAREXXXXDIS-ESSFTP", "User", "111") 2 Dim temvalue As String = "" 3 GetValueFromRegistry("SOFTWAREXXXXDIS-ESSFTP", "User", temvalue) 4 5 'DeleteRegistry("SOFTWAREUnisysDIS-ESSFTP", "User") 6 7 RegistryExist("SOFTWAREXXXXDIS-ESSFTP", "User")