Foros del Web » Programación para mayores de 30 ;) » .NET »

TIP: Como leer datos del registro en VB.NET 2008

Estas en el tema de TIP: Como leer datos del registro en VB.NET 2008 en el foro de .NET en Foros del Web. Saludos. Me he decidido a escribir este post a razón de la escasa documentación respecto al manejo del registro en visual studio 2008. Existen funciones ...
  #1 (permalink)  
Antiguo 02/12/2009, 09:46
 
Fecha de Ingreso: octubre-2009
Mensajes: 10
Antigüedad: 14 años, 7 meses
Puntos: 0
TIP: Como leer datos del registro en VB.NET 2008

Saludos.

Me he decidido a escribir este post a razón de la escasa documentación respecto al manejo del registro en visual studio 2008. Existen funciones y clases creadas para visual basic 6 pero no funciona para la ultima version de visual.net.

Este ejemplo está enfocado a listar las aplicaciones instaladas en el equipo.

Módulo REG32:
Código:
Option Strict Off
Option Explicit On

Module REG32
    'Códigos de error y sus valores: help.netop.com/support/errorcodes/win32_error_codes.htm

    'ADVAPI32 Registry API Bas File.
    ' This file was not writen by me but I like to thank who did write it

    ' --------------------------------------------------------------------
    ' ADVAPI32
    ' --------------------------------------------------------------------
    ' function prototypes, constants, and type definitions
    ' for Windows 32-bit Registry API

    Public Const HKEY_CLASSES_ROOT As Integer = &H80000000
    Public Const HKEY_CURRENT_USER As Integer = &H80000001
    Public Const HKEY_LOCAL_MACHINE As Integer = &H80000002
    Public Const HKEY_USERS As Integer = &H80000003
    Public Const HKEY_PERFORMANCE_DATA As Integer = &H80000004
    Public Const ERROR_SUCCESS As Short = 0
    Public Const ERROR_NONE As Short = 0
    Public sKeys As Collection

Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Integer, ByVal dwIndex As Integer, ByVal lpName As String, _
ByRef lpcbName As Integer, ByVal lpReserved As Integer, ByVal lpClass As String, ByRef lpcbClass As Integer, ByRef lpftLastWriteTime As Integer) As Integer


Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Integer, ByVal lpSubKey As String, ByRef phkResult As Integer) As Integer


Public Sub GetKeyNames(ByVal hKey As Integer, ByVal strPath As String)
        Dim Cnt, TKey As Integer 'Cnt es un contador para acceder a la subclaves, empieza como 0 y se incrementa hasta que se llegue al final
        Dim StrBuff, StrKey As String
        RegOpenKey(hKey, strPath, TKey)
        Do
            StrBuff = New String(vbNullChar, 255) 'A pointer to a buffer that receives the name of the subkey, including the terminating null character
            'RegEnumKeyEx devuelve el valor ERROR_SUCCESS que equivale a 0 cuando tiene exito. Si falla devuelve un codigo de error
            If RegEnumKeyEx(TKey, Cnt, StrBuff, 255, 0, vbNullString, 0, 0) <> 0 Then Exit Do
            Cnt = Cnt + 1
            StrKey = Left(StrBuff, InStr(StrBuff, vbNullChar) - 1)
            sKeys.Add(StrKey)
        Loop
    End Sub

End Module
Más información acerca de las funciones de manejo del registro de windows en:
msdn.microsoft.com/en-us/library/ms724875%28VS.85%29.aspx

Formulario principal. Se carga con el evento LOAD en un listview:

Código:
Private Sub FormNewApp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        sKeys = New Collection
        LoadList()
End Sub

Private Sub LoadList()
        Dim StrDisName, StrPub, StrVersion, StrInstLoc As String
        Dim Icnt As Short
        Dim IntString As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"

        With ListView1
            .Columns.Add("Software", 200, HorizontalAlignment.Center)
            .Columns.Add("Version", 100, HorizontalAlignment.Center)
            .Columns.Add("Location", 200, HorizontalAlignment.Center)
            .Columns.Add("Publisher", 200, HorizontalAlignment.Center)
            .View = View.List
            .GridLines = True
            .BorderStyle = BorderStyle.Fixed3D            
        End With

        REG32.GetKeyNames(HKEY_LOCAL_MACHINE, IntString)
        For Icnt = 1 To sKeys.Count() - 1            
            If (sKeys.Item(Icnt).ToString.StartsWith("KB") = False) Then
                StrDisName = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\" _
                + IntString + sKeys.Item(Icnt), "DisplayName", vbNullChar) 'lectura del nombre
                StrVersion = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\" _
                + IntString + sKeys.Item(Icnt), "DisplayVersion", vbNullChar) 'lectura de la version
                StrInstLoc = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\" _
                + IntString + sKeys.Item(Icnt), "InstallLocation", vbNullChar) 'lectura del directorio de instalacion
                StrPub = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\" _
                + IntString + sKeys.Item(Icnt), "Publisher", vbNullChar) 'lectura del desarrollador
                If (StrPub <> vbNullChar) Then 'filtrado de entradas que no tengan desarrollador y actualizaciones
                    'Las actualizaciones contienen "KB" en DisplayName
                    If (StrDisName <> vbNullChar) And (StrDisName.Contains("KB") = False) Then
                        Dim item As New ListViewItem(StrDisName)
                        If (StrVersion = vbNullChar) Then
                            StrVersion = "------"
                        End If
                        If (StrInstLoc = vbNullChar) Then
                            StrInstLoc = "------"
                        End If
                        If (StrPub = vbNullChar) Then
                            StrPub = "------"
                        End If
                        item.SubItems.Add(StrVersion)
                        item.SubItems.Add(StrInstLoc)
                        item.SubItems.Add(StrPub)
                        ListView1.Items.Add(item)
                    End If
                End If
            End If
        Next
        ListView1.Sorting = SortOrder.Ascending
        sKeys = Nothing
        StrDisName = ""        

    End Sub
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:58.