ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 2388|ตอบกลับ: 1

[VB.NET] การแสดงรายชื่อแอพพลิเคชั่น โปรแกรมต่างๆที่ติดตั้งเอาไว้ใน Windows

[คัดลอกลิงก์]

309

กระทู้

500

โพสต์

6030

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6030




จัดให้ตามคำขอ การแสดงรายชื่อ Software หรือ Application ต่างๆที่ติดตั้งอยู่ในระบบปฏิบัติการ Windows โดยอาศัยหลักการอ่านข้อมูลที่อยู่ภายใน Registry ในส่วนของ Uninstall แถมโค้ดในการแสดงรายชื่อเครื่องพิมพ์ และการตรวจสอบขนาดจำนวนบิทของ Windows ซึ่งอย่างหลังนี่ใช้การอ่านข้อมูลด้วย WMI แทนครับผม


Registry ในส่วนของการ Uninstall ...



มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. Imports Microsoft.Win32
  2. Imports System.Runtime.InteropServices

  3. Public Class frmListSoftware

  4.     Private Sub frmListSoftware_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  5.         Call ListSoftwareFromRegistry()
  6.         Call ListPrinter()
  7.     End Sub

  8.     '// ตรวจสอบ Windows ขนาดกี่บิท
  9.     '// http://msdn.microsoft.com/En-US/library/aa394239.aspx
  10.     Function CheckWinOSBit() As String
  11.         Dim osClass As System.Management.ManagementClass = Nothing
  12.         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.
  13.         Try
  14.             ' Get the singleton Win32_OperatingSystem WMI class so we can access properties about the OS.
  15.             osClass = New System.Management.ManagementClass("Win32_OperatingSystem")
  16.             ' Loop thru all properties of the single instance of the Win32_OperatingSystem class and look for the property which will tell us if
  17.             ' the OS is 32-bit or 64-bit.  If the property is not found, the OS is assumed to be 32-bit.
  18.             For Each mgo As System.Management.ManagementObject In osClass.GetInstances
  19.                 For Each prop As System.Management.PropertyData In mgo.Properties
  20.                     If prop.Name = "OSArchitecture" Then
  21.                         result = prop.Value.ToString
  22.                         Exit For
  23.                     End If
  24.                 Next
  25.             Next
  26.         Catch ex As Exception
  27.             result = String.Empty
  28.         Finally
  29.             ' Clean up
  30.             If osClass IsNot Nothing Then osClass.Dispose()
  31.         End Try
  32.         Return result
  33.     End Function

  34.     '// Listing Software.
  35.     Private Sub ListSoftwareFromRegistry()
  36.         '// Initialize ListView Control
  37.         With ListView1
  38.             .View = View.Details
  39.             .GridLines = True
  40.             .FullRowSelect = True
  41.             .HideSelection = False
  42.             .MultiSelect = False
  43.             .Columns.Add("Program Name", ListView1.Width \ 2 + 200)
  44.             .Columns.Add("Version", 150)
  45.         End With
  46.         Dim LV As ListViewItem
  47.         '// Registry path which has information of all the softwares installed on machine.
  48.         Dim UninstallKey As String
  49.         '// Check Windows 32/64 bit.
  50.         If Microsoft.VisualBasic.Left(CheckWinOSBit, 2) = "64" Then
  51.             '// For 64 bit.
  52.             UninstallKey = "Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
  53.             Label1.Text = "Listing Application Software on Windows 64 bit."
  54.         Else
  55.             '// For 32 bit.
  56.             UninstallKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
  57.             Label1.Text = "Listing Application Software on Windows 32 bit."
  58.         End If
  59.         '// ลูปตาม SubKey
  60.         Using RegKey As RegistryKey = Registry.LocalMachine.OpenSubKey(UninstallKey)
  61.             For Each skName As String In RegKey.GetSubKeyNames()
  62.                 Using sk As RegistryKey = RegKey.OpenSubKey(skName)
  63.                     If Not IsNothing(sk.GetValue("DisplayName")) Then
  64.                         ' we have many attributes other than these which are useful.
  65.                         LV = ListView1.Items.Add(CStr(sk.GetValue("DisplayName")))
  66.                         LV.SubItems.Add(CStr(sk.GetValue("DisplayVersion")))
  67.                     End If
  68.                 End Using
  69.             Next
  70.         End Using
  71.     End Sub

  72.     '// Listing Printers.
  73.     Sub ListPrinter()
  74.         Dim sPrinters As String = ""
  75.         ' Populate to the ListBox.
  76.         For Each sPrinters In System.Drawing.Printing.PrinterSettings.InstalledPrinters
  77.             ListBox1.Items.Add(sPrinters)
  78.         Next
  79.     End Sub

  80.     Private Sub frmListSoftware_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
  81.         ' Resize ListView Control.
  82.         If ListView1.Columns.Count > 0 Then
  83.             With ListView1
  84.                 .Columns(0).Width = ListView1.Width \ 2 + 200
  85.                 .Columns(1).Width = 150
  86.             End With
  87.         End If
  88.     End Sub

  89.     Private Sub frmListSoftware_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  90.         Me.Dispose()
  91.         GC.SuppressFinalize(Me)
  92.         Application.Exit()
  93.     End Sub
  94. End Class
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

58

โพสต์

10

เครดิต

Member

Rank: 2

เครดิต
10
โพสต์ 2022-10-25 15:14:08 | ดูโพสต์ทั้งหมด

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-19 20:09 , Processed in 0.657856 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้