熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

VB.NET中得到計算機硬件信息

2022-06-13   來源: .NET編程 

  本文匯集了中得到計算機硬件信息的一些功能

  得到顯示器分辨率

  Dim X As Short = System<a _blank>Windows</a>FormsScreenPrimaryScreenBoundsWidth
    Dim Y As Short = SystemWindowsFormsScreenPrimaryScreenBoundsHeight
    MsgBox(您的顯示器分辨率是 & X & X & Y)

  得到特殊文件夾的路徑
    Desktop桌面文件夾路徑
    MsgBox(EnvironmentGetFold<a _blank>ERP</a>ath(EnvironmentSpecialFolderDesktopDirectory))
    Favorites收藏夾路徑
    MsgBox(EnvironmentGetFolderPath(EnvironmentSpecialFolderFavorites))
    Application Data路徑
    MsgBox(EnvironmentGetFolderPath(EnvironmentSpecialFolderApplicationData))

  通用寫法
    Dim SPEC As String = EnvironmentGetFolderPath(EnvironmentSpecialFolderXXXXXXX)
    XXXXXXX是特殊文件夾的名字

  得到操作系統版本信息
    MsgBox(EnvironmentOSVersionToString)

  得到當前登錄的用戶名
    MsgBox(EnvironmentUserName)

  得到當前應用程序的路徑
    MsgBox(EnvironmentCurrentDirectory)

  打開和關閉CDROM
    先新建模塊
Module mciAPIModule
  Declare Function mciSendString Lib winmmdll Alias mciSendStringA _
  (ByVal lpstrCommand As String ByVal lpstrReturnString As String _
  ByVal uReturnLength As Integer ByVal hwndCallback As Integer) As Integer
End Module

  打開CDROM
Dim lRet As Long
lRet = mciSendString(set cdAudio door open & )

  關閉CDROM
Dim lRet As Long
lRet = mciSendString(set cdAudio door Closed & )
更多請參見
?url=/library/enus/multimed/mmcmdstr_eycasp

  得到計算機IP和計算機全名
    Dim MYIP As SystemNetIPHostEntry = SystemNetDnsGetHostByName(SystemNetDnsGetHostName)
    MsgBox(您的IP地址 & (MYIPAddressListGetValue()ToString))
    MsgBox(您的計算機全名 & (MYIPHostNameToString))

  使用win_operatingSystem (wmi Class)得到計算機信息
    添加ListBox在Form_Load事件裡並引用systemManagment
    Dim opSearch As New ManagementObjectSearcher(SELECT * FROM Win_OperatingSystem)
    Dim opInfo As ManagementObject
    For Each opInfo In opSearchGet()
  ListBoxItemsAdd(Name: & opInfo(name)ToString())
  ListBoxItemsAdd(Version: & opInfo(version)ToString())
  ListBoxItemsAdd(Manufacturer: & opInfo(manufacturer)ToString())
  ListBoxItemsAdd(Computer name: & opInfo(csname)ToString())
  ListBoxItemsAdd(Windows Directory: & opInfo(windowsdirectory)ToString())
Next

  列出計算機安裝的全部字體並添加到ListBox
    新建Form並添加ListBox和Button
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
Dim fntCollection As InstalledFontCollection = New InstalledFontCollection()
Dim fntFamily() As FontFamily
fntFamily = fntCollectionFamilies
ListBoxItemsClear()
Dim i As Integer =
For i = To fntFamilyLength
  ListBoxItemsAdd(fntFamily(i)Name)
Next
End Sub

  使用Win_Processor列出處理器的信息
Imports SystemManagement
Public Class Form
  Inherits SystemWindowsFormsForm

  #Region Windows 窗體設計器生成的代碼

  Public Sub New()
    MyBaseNew()

  該調用是 Windows 窗體設計器所必需的
    InitializeComponent()

  在 InitializeComponent() 調用之後添加任何初始化

  End Sub

  窗體重寫 dispose 以清理組件列表
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        componentsDispose()
      End If
    End If
    MyBaseDispose(disposing)
  End Sub

  Windows 窗體設計器所必需的
  Private components As SystemComponentModelIContainer

  注意: 以下過程是 Windows 窗體設計器所必需的
  可以使用 Windows 窗體設計器修改此過程
  不要使用代碼編輯器修改它
  Friend WithEvents ListBox As SystemWindowsFormsListBox
  Friend WithEvents Button As SystemWindowsFormsButton
  <SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()
    MeListBox = New SystemWindowsFormsListBox
    MeButton = New SystemWindowsFormsButton
    MeSuspendLayout()
   
    ListBox
   
    MeListBoxLocation = New SystemDrawingPoint( )
    MeListBoxName = ListBox
    MeListBoxSize = New SystemDrawingSize( )
    MeListBoxTabIndex =
   
    Button
   
    MeButtonLocation = New SystemDrawingPoint( )
    MeButtonName = Button
    MeButtonSize = New SystemDrawingSize( )
    MeButtonTabIndex =
    MeButtonText = 裝載計算機處理器信息
   
    Form
   
    MeAutoScaleBaseSize = New SystemDrawingSize( )
    MeClientSize = New SystemDrawingSize( )
    MeControlsAddRange(New SystemWindowsFormsControl() {MeButton MeListBox})
    MeText = 計算機處理器信息
    MeResumeLayout(False)

  End Sub

  #End Region

  Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) _
   Handles ButtonClick

  Dim ProcQuery As New SelectQuery(Win_Processor)
    Dim ProcSearch As New ManagementObjectSearcher(ProcQuery)
    Dim ProcInfo As ManagementObject

  For Each ProcInfo In ProcSearchGet()
      Call processorfamily(ProcInfo(Family)ToString)
      ListBoxItemsAdd(Description: & ProcInfo(Description)ToString())
      ListBoxItemsAdd(caption: & ProcInfo(caption)ToString())
      ListBoxItemsAdd(Architecture: & ProcInfo(Architecture)ToString())
      Call processortype(ProcInfo(ProcessorType)ToString())
      Call CpuStat(ProcInfo(CpuStatus)ToString)
      ListBoxItemsAdd(MaxClockSpeed: & ProcInfo(MaxClockSpeed)ToString() & MHZ)
      ListBoxItemsAdd(LCacheSpeed: & ProcInfo(LCacheSpeed)ToString() & MHZ)
      ListBoxItemsAdd(ExtClock: & ProcInfo(LCacheSpeed)ToString() & MHZ)
      ListBoxItemsAdd(ProcessorId: & ProcInfo(ProcessorId)ToString())
      ListBoxItemsAdd(AddressWidth: & ProcInfo(AddressWidth)ToString() & Bits)
      ListBoxItemsAdd(DataWidth: & ProcInfo(DataWidth)ToString() & Bits)
      ListBoxItemsAdd(Version: & ProcInfo(Version)ToString())
      ListBoxItemsAdd(ExtClock: & ProcInfo(ExtClock)ToString() & MHZ)
    Next
  End Sub
  Function processorfamily(ByVal procssfam)
    Dim processtype
    Select Case procssfam
      Case
        processtype = Other
      Case
        processtype = Unknown
      Case
        processtype =
      Case
        processtype =
      Case
        processtype =
      Case
        processtype =
      Case
        processtype =
      Case
        processtype =
      Case
        processtype =
      Case
        processtype =
      Case
        processtype = Pentium brand
      Case
        processtype = Pentium Pro
      Case
        processtype = Pentium II
      Case
        processtype = Pentium processor with MMX technology
      Case
        processtype = Celeron
      Case
        processtype = Pentium II Xeon
      Case
        processtype = Pentium III
      Case
        processtype = M Family
      Case
        processtype = M Family
      Case
        processtype = K Family
      Case
        processtype = K Family
      Case
        processtype = K
      Case
        processtype = K
      Case
        processtype = AMD Athlon Processor Family
      Case
        processtype = AMD Duron Processor
      Case
        processtype = AMD Family
      Case
        processtype = K+
      Case
        processtype = Power PC Family
      Case
        processtype = Power PC
      Case
        processtype = Power PC
      Case
        processtype = Power PC +
      Case
        processtype = Power PC
      Case
        processtype = Power PC
      Case
        processtype = Power PC X
      Case
        processtype = Power PC
      Case
        processtype = Alpha Family
      Case
        processtype = Alpha
      Case
        processtype = Alpha
      Case
        processtype = Alpha
      Case
        processtype = Alpha PC
      Case
        processtype = Alpha a
      Case
        processtype = Alpha
      Case
        processtype = Alpha
      Case
        processtype = MIPS Family
      Case
        processtype = MIPS R
      Case
        processtype = MIPS R
      Case
        processtype = MIPS R
      Case
        processtype = MIPS R
      Case
        processtype = MIPS R
      Case
        processtype = SPARC Family
      Case
        processtype = SuperSPARC
      Case
        processtype = microSPARC II
      Case
        processtype = microSPARC IIep
      Case
        processtype = UltraSPARC
      Case
        processtype = UltraSPARC II
      Case
        processtype = UltraSPARC IIi
      Case
        processtype = UltraSPARC III
      Case
        processtype = UltraSPARC IIIi
      Case
        processtype =
      Case
        processtype = xxx Family
      Case
        processtype =
      Case
        processtype =
      Case
        processtype =
      Case
        processtype =
      Case
        processtype = Hobbit Family
      Case
        processtype = Crusoe TM Family
      Case
        processtype = Crusoe TM Family
      Case
        processtype = Weitek
      Case
        processtype = Itanium Processor
      Case
        processtype = PARISC Family
      Case
        processtype = PARISC
      Case
        processtype = PARISC
      Case
        processtype = PARISC LC
      Case
        processtype = PARISC
      Case
        processtype = PARISC LC
      Case
        processtype = PARISC
      Case
        processtype = V Family
      Case
        processtype = Pentium III Xeon
      Case
        processtype = Pentium III Processor with Intel SpeedStep Technology
      Case
        processtype = Pentium
      Case
        processtype = Intel Xeon
      Case
        processtype = AS Family
      Case
        processtype = Intel Xeon processor MP
      Case
        processtype = AMD AthlonXP Family
      Case
        processtype = AMD AthlonMP Family
      Case
        processtype = Intel Itanium
      Case
        processtype = AMD Opteron Family
      Case
        processtype = K
      Case
        processtype = IBM Family
      Case
        processtype = G
      Case
        processtype = G
      Case
        processtype = i
      Case
        processtype = i
      Case
        processtype = SH
      Case
        processtype = SH
      Case
        processtype = arm
      Case
        processtype = Strongarm
      Case
        processtype = x
      Case
        processtype = MediaGX
      Case
        processtype = MII
      Case
        processtype = WinChip
      Case
        processtype = DSP
      Case
        processtype = Video Processor
    End Select
    ListBoxItemsAdd(Family: & processtype)

  End Function
  Function CpuStat(ByVal CpuStNUM)
    Dim stat
    Select Case CpuStNUM
      Case
        stat = Unknown
      Case
        stat = CPU Enabled
      Case
        stat = CPU Disabled by User via BIOS Setup
      Case
        stat = CPU Disabled By BIOS (POST Error)
      Case
        stat = CPU is Idle
      Case
        stat = Reserved
      Case
        stat = Reserved
      Case
        stat = Other
    End Select
    ListBoxItemsAdd(CpuStatus: & stat)
  End Function
  Function processortype(ByVal proctypenum)
    Dim proctype
    Select Case proctypenum
      Case
        proctype = Other
      Case
        proctype = Unknown
      Case
        proctype = Central Processor
      Case
        proctype = Math Processor
      Case
        proctype = DSP Processor
      Case
        proctype = Video Processor
    End Select
    ListBoxItemsAdd(Processor Type: & proctype)

  End Function
End Class

  得到CDROM信息
Imports SystemManagement
Public Class Form
    Inherits SystemWindowsFormsForm

  #Region Windows 窗體設計器生成的代碼

  Public Sub New()
    MyBaseNew()

  該調用是 Windows 窗體設計器所必需的
    InitializeComponent()

  在 InitializeComponent() 調用之後添加任何初始化

  End Sub

  窗體重寫 dispose 以清理組件列表
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        componentsDispose()
      End If
    End If
    MyBaseDispose(disposing)
  End Sub

  Windows 窗體設計器所必需的
  注意: 以下過程是 Windows 窗體設計器所必需的
  可以使用 Windows 窗體設計器修改此過程
  不要使用代碼編輯器修改它
  Private components As SystemComponentModelIContainer
  Friend WithEvents ListBox As SystemWindowsFormsListBox
  <SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()
    MeListBox = New SystemWindowsFormsListBox
    MeSuspendLayout()
   
    ListBox
   
    MeListBoxLocation = New SystemDrawingPoint( )
    MeListBoxName = ListBox
    MeListBoxSize = New SystemDrawingSize( )
    MeListBoxTabIndex =
   
    Form
   
    MeAutoScaleBaseSize = New SystemDrawingSize( )
    MeClientSize = New SystemDrawingSize( )
    MeControlsAddRange(New SystemWindowsFormsControl() {MeListBox})
    MeName = Form
    MeText = Form
    MeResumeLayout(False)

  End Sub

  #End Region

  Private Sub Form_Load(ByVal sender As SystemObject ByVal e As SystemEventArgs) _
   Handles MyBaseLoad
    On Error Resume Next
    Dim SoundDeviceQuery As New SelectQuery(Win_CDROMDrive)
    Dim SoundDeviceSearch As New ManagementObjectSearcher(SoundDeviceQuery)
    Dim SoundDeviceInfo As ManagementObject
    For Each SoundDeviceInfo In SoundDeviceSearchGet()
      Dim SizeInMBs As Long = (Val(SoundDeviceInfo(Size)ToString()))
      SizeInMBs = Int((SizeInMBs / ( * )))
      ListBoxItemsAdd(CDRom Description: & SoundDeviceInfo(caption)ToString())
      ListBoxItemsAdd(CDRom Manufacturer: & SoundDeviceInfo(Manufacturer)ToString())
      ListBoxItemsAdd(CDRom Drive: & SoundDeviceInfo(drive)ToString())
      ListBoxItemsAdd(CDRom Media Loaded: & SoundDeviceInfo(MediaLoaded)ToString())
      ListBoxItemsAdd(CDRom Media Type: & SoundDeviceInfo(MediaType)ToString())
      ListBoxItemsAdd(CDRom Volume Name: & SoundDeviceInfo(VolumeName)ToString())
      ListBoxItemsAdd(CDRom Size: & SizeInMBs & MBytes)
      ListBoxItemsAdd(CDRom Status: & SoundDeviceInfo(Status)ToString())
      ListBoxItemsAdd(CDRom MaxMediaSize: & SoundDeviceInfo(MaxMediaSize)ToString())
      ListBoxItemsAdd(CDRom Id: & SoundDeviceInfo(Id)ToString())
      ListBoxItemsAdd(CDRom TransferRate: +Int(SoundDeviceInfo(TransferRate)ToString())+ KBs/秒)
    Next
  End Sub
End Class

  得到硬盤信息
Imports SystemManagement
Public Class Form
    Inherits SystemWindowsFormsForm

  #Region Windows Form Designer generated code

  Public Sub New()
    MyBaseNew()
    InitializeComponent()
  End Sub

  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        componentsDispose()
      End If
    End If
    MyBaseDispose(disposing)
  End Sub
  Private components As SystemComponentModelIContainer
  Friend WithEvents ListBox As SystemWindowsFormsListBox
  <SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()
    MeListBox = New SystemWindowsFormsListBox
    MeSuspendLayout()
   
    ListBox
   
    MeListBoxLocation = New SystemDrawingPoint( )
    MeListBoxName = ListBox
    MeListBoxSize = New SystemDrawingSize( )
    MeListBoxTabIndex =
   
    Form
   
    MeAutoScaleBaseSize = New SystemDrawingSize( )
    MeClientSize = New SystemDrawingSize( )
    MeControlsAddRange(New SystemWindowsFormsControl() {MeListBox})
    MeName = Form
    MeText = Form
    MeResumeLayout(False)

  End Sub

  #End Region

  Private Sub Form_Load(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles MyBaseLoad

  On Error Resume Next
    Dim HDDDeviceQuery As New SelectQuery(Win_DiskDrive)
    Dim HDDDeviceSearch As New ManagementObjectSearcher(HDDDeviceQuery)
    Dim HDDDeviceInfo As ManagementObject
    For Each HDDDeviceInfo In HDDDeviceSearchGet()
      ListBoxItemsAdd(HDD Description: & HDDDeviceInfo(caption)ToString())
      ListBoxItemsAdd(HDD BytesPerSector: & HDDDeviceInfo(BytesPerSector)ToString())
      ListBoxItemsAdd(HDD CompressionMethod: & HDDDeviceInfo(CompressionMethod)ToString())
      ListBoxItemsAdd(HDD Index: & HDDDeviceInfo(Index)ToString())
      ListBoxItemsAdd(HDD InstallDate: & HDDDeviceInfo(InstallDate)ToString())
      ListBoxItemsAdd(HDD Manufacturer: & HDDDeviceInfo(Manufacturer)ToString())
      ListBoxItemsAdd(HDD Partitions: & HDDDeviceInfo(Partitions)ToString())
      ListBoxItemsAdd(HDD Size: & Int(Val(HDDDeviceInfo(Size)ToString()) / ^ ) &   GBytes)
      ListBoxItemsAdd(HDD TotalCylinders: & HDDDeviceInfo(TotalCylinders)ToString())
      ListBoxItemsAdd(HDD TotalSectors: & HDDDeviceInfo(TotalSectors)ToString())
      ListBoxItemsAdd(HDD TracksPerCylinder: & HDDDeviceInfo(TracksPerCylinder)ToString())
      ListBoxItemsAdd(HDD TotalHeads: & HDDDeviceInfo(TotalHeads)ToString())
      ListBoxItemsAdd(HDD TotalTracks: & HDDDeviceInfo(TotalTracks)ToString())
      ListBoxItemsAdd(HDD SectorsPerTrack: & HDDDeviceInfo(SectorsPerTrack)ToString())
      ListBoxItemsAdd(HDD SCSILogicalUnit: & HDDDeviceInfo(SCSILogicalUnit)ToString())
    Next
  End Sub
End Class

  得到聲卡信息
Imports SystemManagement
Public Class Form
    Inherits SystemWindowsFormsForm

  #Region Windows Form Designer generated code

  Public Sub New()
    MyBaseNew()
    InitializeComponent()
  End Sub

  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        componentsDispose()
      End If
    End If
    MyBaseDispose(disposing)
  End Sub
  Private components As SystemComponentModelIContainer
  Friend WithEvents ListBox As SystemWindowsFormsListBox
  <SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()
    MeListBox = New SystemWindowsFormsListBox
    MeSuspendLayout()
   
    ListBox
   
    MeListBoxLocation = New SystemDrawingPoint( )
    MeListBoxName = ListBox
    MeListBoxSize = New SystemDrawingSize( )
    MeListBoxTabIndex =
   
    Form
   
    MeAutoScaleBaseSize = New SystemDrawingSize( )
    MeClientSize = New SystemDrawingSize( )
    MeControlsAddRange(New SystemWindowsFormsControl() {MeListBox})
    MeName = Form
    MeText = Form
    MeResumeLayout(False)

  End Sub

  #End Region

  Private Sub Form_Load(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles MyBaseLoad
    Dim SoundDeviceQuery As New SelectQuery(Win_SoundDevice)
    Dim SoundDeviceSearch As New ManagementObjectSearcher(SoundDeviceQuery)
    Dim SoundDeviceInfo As ManagementObject
    For Each SoundDeviceInfo In SoundDeviceSearchGet()
      ListBoxItemsAdd(Sound Device Description: & SoundDeviceInfo(Caption)ToString())
      ListBoxItemsAdd(Sound Device Status: & SoundDeviceInfo(status)ToString())
      ListBoxItemsAdd(Sound Device Manufacturer: & SoundDeviceInfo(Manufacturer)ToString())
    Next
  End Sub

  End Class


From:http://tw.wingwit.com/Article/program/net/201311/11413.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.