VB.NET-Quellcode
- 'Beispiel: VB .Net - Registrierdatenbank - Installierte Software ermitteln
- '
- Option Explicit On
- Option Strict On
- Imports Microsoft.Win32
- Imports Microsoft.VisualBasic.ControlChars
- Imports System.Globalization
- Public Class Form1
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles Button1.Click
- GetInstalledSoftware(My.Computer.Name)
- End Sub
- Private Sub GetInstalledSoftware(ByVal machineName As String)
- Dim hKey As RegistryKey
- Dim SubKey As RegistryKey
- Dim Path As String = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
- Dim Items As String()
- Dim DisplayName As String
- Dim InstallSource As String
- Dim InstallDate As String
- Try
- hKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName)
- SubKey = hKey.CreateSubKey(Path)
- Items = SubKey.GetSubKeyNames()
- For n As Integer = 0 To Items.Length - 1
- Dim rk As RegistryKey = SubKey.OpenSubKey(Items(n), False)
- Try
- DisplayName = (rk.GetValue("DisplayName").ToString())
- InstallDate = (rk.GetValue("InstallDate").ToString())
- InstallSource = (rk.GetValue("InstallSource").ToString())
- Me.ListBox1.Items.Add(DisplayName & Tab & Tab & ConvertStringToDate(InstallDate) & _
- Tab & Tab & InstallSource)
- Catch
- End Try
- Next
- hKey.Close()
- Catch ex As Exception
- End Try
- End Sub
- Private Function ConvertStringToDate(ByVal s As String) As String
- Try
- Dim k As Integer = s.IndexOf(".")
- Dim j As Integer = s.IndexOf("/")
- Dim i As Integer = s.IndexOf("\")
- Dim n As Integer = s.IndexOf(":")
- Const DateLength As Integer = 8
- If k > -1 Or j > -1 Or i > -1 Or n > -1 Then
- Return s
- Else
- If s.Length = DateLength Then
- Dim Result As String = s.Substring(0, 4) & "/" & s.Substring(4, 2) & "/" & _
- s.Substring(6, 2)
- Dim ci As CultureInfo = CultureInfo.InvariantCulture
- Dim dt As DateTime = DateTime.ParseExact(Result, "yyyy/MM/dd", _
- ci, DateTimeStyles.NoCurrentDateDefault)
- Return dt.ToShortDateString()
- Else
- End If
- End If
- Catch ex As Exception
- Return "-"
- End Try
- Return ""
- End Function
- Private Sub ListBoxInitTabStops(ByRef lb As System.Windows.Forms.ListBox)
- Try
- If (Not lb Is Nothing) Then
- Dim cntTabs() As Integer = {200, 210, 220}
- Call Win32Api.SendMessage(lb.Handle, Win32Api.LB_SETTABSTOPS, _
- cntTabs.Length, cntTabs(0))
- End If
- Catch ex As Exception
- MessageBox.Show(ex.Message.ToString(), "Info")
- End Try
- End Sub
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles MyBase.Load
- Call ListBoxInitTabStops(ListBox1)
- End Sub
- Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles Button2.Click
- GetInstalledSoftwareExeFiles(My.Computer.Name)
- End Sub
- Private Sub GetInstalledSoftwareExeFiles(ByVal machineName As String)
- Dim hKey As RegistryKey
- Dim SubKey As RegistryKey
- Dim Path As String = "Software\Microsoft\Windows\CurrentVersion\App Paths"
- Dim Items As String()
- Dim exePath As String
- Try
- hKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName)
- SubKey = hKey.CreateSubKey(Path)
- Items = SubKey.GetSubKeyNames()
- For n As Integer = 0 To Items.Length - 1
- Dim rk As RegistryKey = SubKey.OpenSubKey(Items(n), False)
- Try
- exePath = (rk.GetValue("").ToString())
- If exePath.IndexOf(Quote) > -1 Then exePath = exePath.Replace(Quote, "")
- If exePath.LastIndexOf(Quote) > -1 Then exePath = exePath.Replace(Quote, "")
- Dim Root As String = IO.Path.GetPathRoot(exePath)
- If DirectoryExists(Root) = True Then
- If IO.File.Exists(exePath) Then
- Me.ListBox2.Items.Add(exePath.ToLower())
- Else
- Debug.WriteLine(exePath)
- End If
- Else
- Debug.WriteLine(exePath)
- End If
- Catch
- End Try
- Next
- hKey.Close()
- Catch ex As Exception
- End Try
- End Sub
- Private Function DirectoryExists(ByVal Root As String) As Boolean
- Try
- Dim files() As String = IO.Directory.GetFiles(Root, "*.*")
- If files.Length < 1 Then
- Return False
- Else
- Return True
- End If
- Catch
- Return False
- End Try
- End Function
- Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles ListBox2.SelectedIndexChanged
- Try
- Dim psi As New ProcessStartInfo()
- With psi
- .ErrorDialog = True
- .FileName = CStr(ListBox2.SelectedItem)
- .UseShellExecute = True
- End With
- Process.Start(psi)
- Catch
- End Try
- End Sub
- End Class
- Public Module Win32Api
- #Region "Required Windows Application Interface"
- <Runtime.InteropServices.DllImport("user32.dll")> _
- Public Sub SendMessage( _
- ByVal hWnd As IntPtr, _
- ByVal uMsg As Int32, _
- ByVal wParam As Int32, _
- ByRef lParam As Int32)
- End Sub
- #End Region
- #Region "Required Windows Constants"
- Public Const LB_SETTABSTOPS As Int32 = &H192
- #End Region
- End Module
link zu Code
Mfg Alex