[VB.NET] การแสดงรายชื่อแอพพลิเคชั่น โปรแกรมต่างๆที่ติดตั้งเอาไว้ใน Windows
http://www.g2gnet.com/webboard/images/vbnet/ListSoftware.pngจัดให้ตามคำขอ การแสดงรายชื่อ Software หรือ Application ต่างๆที่ติดตั้งอยู่ในระบบปฏิบัติการ Windows โดยอาศัยหลักการอ่านข้อมูลที่อยู่ภายใน Registry ในส่วนของ Uninstall แถมโค้ดในการแสดงรายชื่อเครื่องพิมพ์ และการตรวจสอบขนาดจำนวนบิทของ Windows ซึ่งอย่างหลังนี่ใช้การอ่านข้อมูลด้วย WMI แทนครับผม
Registry ในส่วนของการ Uninstall ...
http://www.g2gnet.com/webboard/images/vbnet/ListSoftwareregistry.png
มาดูโค้ดฉบับเต็มกันเถอะ ...
Imports Microsoft.Win32
Imports System.Runtime.InteropServices
Public Class frmListSoftware
Private Sub frmListSoftware_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Call ListSoftwareFromRegistry()
Call ListPrinter()
End Sub
'// ตรวจสอบ Windows ขนาดกี่บิท
'// http://msdn.microsoft.com/En-US/library/aa394239.aspx
Function CheckWinOSBit() As String
Dim osClass As System.Management.ManagementClass = Nothing
Dim result As String = "32-bit" ' Default to 32-bit for OSes which don't support the OSArchitecture property of the Win32_OperatingSystem WMI class.
Try
' Get the singleton Win32_OperatingSystem WMI class so we can access properties about the OS.
osClass = New System.Management.ManagementClass("Win32_OperatingSystem")
' Loop thru all properties of the single instance of the Win32_OperatingSystem class and look for the property which will tell us if
' the OS is 32-bit or 64-bit.If the property is not found, the OS is assumed to be 32-bit.
For Each mgo As System.Management.ManagementObject In osClass.GetInstances
For Each prop As System.Management.PropertyData In mgo.Properties
If prop.Name = "OSArchitecture" Then
result = prop.Value.ToString
Exit For
End If
Next
Next
Catch ex As Exception
result = String.Empty
Finally
' Clean up
If osClass IsNot Nothing Then osClass.Dispose()
End Try
Return result
End Function
'// Listing Software.
Private Sub ListSoftwareFromRegistry()
'// Initialize ListView Control
With ListView1
.View = View.Details
.GridLines = True
.FullRowSelect = True
.HideSelection = False
.MultiSelect = False
.Columns.Add("Program Name", ListView1.Width \ 2 + 200)
.Columns.Add("Version", 150)
End With
Dim LV As ListViewItem
'// Registry path which has information of all the softwares installed on machine.
Dim UninstallKey As String
'// Check Windows 32/64 bit.
If Microsoft.VisualBasic.Left(CheckWinOSBit, 2) = "64" Then
'// For 64 bit.
UninstallKey = "Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
Label1.Text = "Listing Application Software on Windows 64 bit."
Else
'// For 32 bit.
UninstallKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
Label1.Text = "Listing Application Software on Windows 32 bit."
End If
'// ลูปตาม SubKey
Using RegKey As RegistryKey = Registry.LocalMachine.OpenSubKey(UninstallKey)
For Each skName As String In RegKey.GetSubKeyNames()
Using sk As RegistryKey = RegKey.OpenSubKey(skName)
If Not IsNothing(sk.GetValue("DisplayName")) Then
' we have many attributes other than these which are useful.
LV = ListView1.Items.Add(CStr(sk.GetValue("DisplayName")))
LV.SubItems.Add(CStr(sk.GetValue("DisplayVersion")))
End If
End Using
Next
End Using
End Sub
'// Listing Printers.
Sub ListPrinter()
Dim sPrinters As String = ""
' Populate to the ListBox.
For Each sPrinters In System.Drawing.Printing.PrinterSettings.InstalledPrinters
ListBox1.Items.Add(sPrinters)
Next
End Sub
Private Sub frmListSoftware_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
' Resize ListView Control.
If ListView1.Columns.Count > 0 Then
With ListView1
.Columns(0).Width = ListView1.Width \ 2 + 200
.Columns(1).Width = 150
End With
End If
End Sub
Private Sub frmListSoftware_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Me.Dispose()
GC.SuppressFinalize(Me)
Application.Exit()
End Sub
End Class
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...
ขอบคุณครับ
หน้า:
[1]