Windows Forms - Funções de acesso ao Registry do Windows
Nota : As funções de acesso ao registry do windows são perigosas podendo destruir a instalação do
windows.
Por esse motivo só deve ser utilizada em caso de extrema necessidade e muitos anti-virus e o
próprio sistema operacional bloqueiam essas funcionalidades.
Com certeza precisará de privilégios administrativos de administrador do windows para utilizar estes
recursos.
Imports Microsoft.Win32
Module Registry1
Public Class Class1
''' <summary>
''' Exemplo : Dim sAns As String
''' sAns = x.RegValue(RegistryHive.LocalMachine, "HARDWARE\DESCRIPTION\System\BIOS", "BaseBoardManufacturer", sErr)
''' sAns = x.RegValue(RegistryHive.LocalMachine, "HARDWARE\DESCRIPTION\System\BIOS", "BaseBoardProduct", sErr)
''' sAns = x.RegValue(RegistryHive.LocalMachine, "HARDWARE\DESCRIPTION\System\BIOS", "BIOSVendor", sErr)
''' </summary>
''' <param name="Hive"></param>
''' <param name="Key"></param>
''' <param name="ValueName"></param>
''' <param name="ErrInfo"></param>
''' <returns></returns>
Public Function RegValue(ByVal Hive As RegistryHive, ByVal Key As String, ByVal ValueName As String, Optional ByRef ErrInfo As String = "") As String
Dim objParent As RegistryKey
Dim objSubkey As RegistryKey
Dim sAns As String
sAns = ""
Try
Select Case Hive
Case RegistryHive.ClassesRoot
objParent = Registry.ClassesRoot
Case RegistryHive.CurrentConfig
objParent = Registry.CurrentConfig
Case RegistryHive.CurrentUser
objParent = Registry.CurrentUser
'Case RegistryHive.DynData win95/98
'objParent = Registry.DynData win95/98
Case RegistryHive.LocalMachine
objParent = Registry.LocalMachine
Case RegistryHive.PerformanceData
objParent = Registry.PerformanceData
Case RegistryHive.Users
objParent = Registry.Users
objSubkey = objParent.OpenSubKey(Key)
'if can't be found, object is not initialized
If Not objSubkey Is Nothing Then
sAns = (objSubkey.GetValue(ValueName))
End If
End Select
Catch ex As Exception
ErrInfo = ex.Message
Finally
'if no error but value is empty, populate errinfo
If ErrInfo = "" And sAns = "" Then
ErrInfo = "Não foi encontrado valor algum para a chave fornecida"
End If
End Try
Return sAns
End Function
''' <summary>
''' Exemplo : Dim sAns As String
''' sAns = x.RegKey(RegistryHive.ClassesRoot, "MIME\Database\Content Type", sErr)
''' </summary>
''' <param name="Hive"></param>
''' <param name="Key"></param>
''' <param name="ErrInfo"></param>
''' <returns></returns>
Public Function RegKey(ByVal Hive As RegistryHive, ByVal Key As String, Optional ByRef ErrInfo As String = "") As String
Dim objParent As RegistryKey
Dim objSubkey As RegistryKey
Dim sAns As String
sAns = ""
Try
Select Case Hive
Case RegistryHive.ClassesRoot
objParent = Registry.ClassesRoot
Case RegistryHive.CurrentConfig
objParent = Registry.CurrentConfig
Case RegistryHive.CurrentUser
objParent = Registry.CurrentUser
'Case RegistryHive.DynData 'win95/98
'objParent = Registry.DynData 'win95/98
Case RegistryHive.LocalMachine
objParent = Registry.LocalMachine
Case RegistryHive.PerformanceData
objParent = Registry.PerformanceData
Case RegistryHive.Users
objParent = Registry.Users
objSubkey = objParent.OpenSubKey(Key)
'if can't be found, object is not initialized
If Not objSubkey Is Nothing Then
For Each valueName As String In objSubkey.GetSubKeyNames()
sAns = sAns + valueName + vbCr
Next
End If
End Select
Catch ex As Exception
ErrInfo = ex.Message
Finally
'if no error but value is empty, populate errinfo
If ErrInfo = "" And sAns = "" Then
ErrInfo = "Não foi encontrado valor algum para a chave fornecida"
End If
End Try
Return sAns
End Function
End Class
End Module