首页 > 编程 > .NET > 正文

获取计算机硬件信息的VB.NET代码范例

2024-07-10 13:07:14
字体:
来源:转载
供稿:网友

得到显示器分辨率


   本文相关代码如下:
dim x as short = system.windows.forms.screen.primaryscreen.bounds.width
dim y as short = system.windows.forms.screen.primaryscreen.bounds.height
msgbox("您的显示器分辨率是:" & x & " x " & y)

 

得到特殊文件夹的路径


   本文相关代码如下:
'"desktop"桌面文件夹路径
msgbox(environment.getfolderpath(environment.specialfolder.desktopdirectory))
'"favorites"收藏夹路径
msgbox(environment.getfolderpath(environment.specialfolder.favorites))
'"application data"路径
msgbox(environment.getfolderpath(environment.specialfolder.applicationdata))
'通用写法
'dim spec as string = environment.getfolderpath(environment.specialfolder.xxxxxxx)
'xxxxxxx是特殊文件夹的名字

 

得到操作系统版本信息


   本文相关代码如下:
msgbox(environment.osversion.tostring)

 

得到当前登录的用户名


   本文相关代码如下:
msgbox(environment.username)

 

得到当前应用程序的路径


   本文相关代码如下:
msgbox(environment.currentdirectory)

 

打开和关闭cd-rom


   本文相关代码如下:
'先新建模块
module mciapimodule
declare function mcisendstring lib "winmm.dll" alias "mcisendstringa" _
(byval lpstrcommand as string, byval lpstrreturnstring as string, _
byval ureturnlength as integer, byval hwndcallback as integer) as integer
end module
'打开cd-rom
dim lret as long
lret = mcisendstring("set cdaudio door open", 0&, 0, 0)
'关闭cd-rom
dim lret as long
lret = mcisendstring("set cdaudio door closed", 0&, 0, 0)
'更多请参见
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/mmcmdstr_8eyc.asp

 

得到计算机ip和计算机全名


   本文相关代码如下:
dim myip as system.net.iphostentry = system.net.dns.gethostbyname(system.net.dns.gethostname)
msgbox("您的ip地址:" & (myip.addresslist.getvalue(0).tostring))
msgbox("您的计算机全名:" & (myip.hostname.tostring))


使用win32_operatingsystem (wmi class)得到计算机信息


   本文相关代码如下:
'添加listbox在form1_load事件里,并引用system.managment
dim opsearch as new managementobjectsearcher("select * from win32_operatingsystem")
dim opinfo as managementobject
for each opinfo in opsearch.get()
listbox1.items.add("name: " & opinfo("name").tostring())
listbox1.items.add("version: " & opinfo("version").tostring())
listbox1.items.add("manufacturer: " & opinfo("manufacturer").tostring())
listbox1.items.add("computer name: " & opinfo("csname").tostring())
listbox1.items.add("windows directory: " & opinfo("windowsdirectory").tostring())
next

 

列出计算机安装的全部字体,并添加到listbox


   本文相关代码如下:
'新建form并添加listbox和button
private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
dim fntcollection as installedfontcollection = new installedfontcollection()
dim fntfamily() as fontfamily
fntfamily = fntcollection.families
listbox1.items.clear()
dim i as integer = 0
for i = 0 to fntfamily.length - 1
listbox1.items.add(fntfamily(i).name)
next
end sub

 

使用win32_processor列出处理器的信息


   本文相关代码如下:
imports system.management
public class form1
inherits system.windows.forms.form
#region " windows 窗体设计器生成的代码 "
public sub new()
  mybase.new()
  '该调用是 windows 窗体设计器所必需的。
  initializecomponent()
  '在 initializecomponent() 调用之后添加任何初始化
end sub
'窗体重写 dispose 以清理组件列表。
protected overloads overrides sub dispose(byval disposing as boolean)
  if disposing then
    if not (components is nothing) then
      components.dispose()
    end if
  end if
  mybase.dispose(disposing)
end sub
'windows 窗体设计器所必需的
private components as system.componentmodel.icontainer
'注意: 以下过程是 windows 窗体设计器所必需的
'可以使用 windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
friend withevents listbox1 as system.windows.forms.listbox
friend withevents button1 as system.windows.forms.button
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
  me.listbox1 = new system.windows.forms.listbox
  me.button1 = new system.windows.forms.button
  me.suspendlayout()
  '
  'listbox1
  '
  me.listbox1.location = new system.drawing.point(8, 8)
  me.listbox1.name = "listbox1"
  me.listbox1.size = new system.drawing.size(280, 186)
  me.listbox1.tabindex = 0
  '
  'button1
  '
  me.button1.location = new system.drawing.point(56, 208)
  me.button1.name = "button1"
  me.button1.size = new system.drawing.size(168, 32)
  me.button1.tabindex = 1
  me.button1.text = "装载计算机处理器信息"
  '
  'form1
  '
  me.autoscalebasesize = new system.drawing.size(5, 13)
  me.clientsize = new system.drawing.size(296, 254)
  me.controls.addrange(new system.windows.forms.control() {me.button1, me.listbox1})
  me.text = "计算机处理器信息"
  me.resumelayout(false)
end sub
#end region
private sub button1_click(byval sender as system.object, byval e as system.eventargs) _
    handles button1.click
  dim procquery as new selectquery("win32_processor")
  dim procsearch as new managementobjectsearcher(procquery)
  dim procinfo as managementobject
  for each procinfo in procsearch.get()
    call processorfamily(procinfo("family").tostring)
    listbox1.items.add("description: " & procinfo("description").tostring())
    listbox1.items.add("caption: " & procinfo("caption").tostring())
    listbox1.items.add("architecture: " & procinfo("architecture").tostring())
    call processortype(procinfo("processortype").tostring())
    call cpustat(procinfo("cpustatus").tostring)
    listbox1.items.add("maxclockspeed: " & procinfo("maxclockspeed").tostring() & "mhz")
    listbox1.items.add("l2cachespeed: " & procinfo("l2cachespeed").tostring() & "mhz")
    listbox1.items.add("extclock: " & procinfo("l2cachespeed").tostring() & "mhz")
    listbox1.items.add("processorid: " & procinfo("processorid").tostring())
    listbox1.items.add("addresswidth: " & procinfo("addresswidth").tostring() & "bits")
    listbox1.items.add("datawidth: " & procinfo("datawidth").tostring() & "bits")
    listbox1.items.add("version: " & procinfo("version").tostring())
    listbox1.items.add("extclock: " & procinfo("extclock").tostring() & "mhz")
  next
end sub
function processorfamily(byval procssfam)
  dim processtype
  select case procssfam
    case 1
      processtype = "other"
    case 2
      processtype = "unknown "
    case 3
      processtype = "8086 "
    case 4
      processtype = "80286 "
    case 5
      processtype = "80386 "
    case 6
      processtype = "80486 "
    case 7
      processtype = "8087 "
    case 8
      processtype = "80287 "
    case 9
      processtype = "80387 "
    case 10
      processtype = "80487 "
    case 11
      processtype = "pentium brand "
    case 12
      processtype = "pentium pro "
    case 13
      processtype = "pentium ii "
    case 14
      processtype = "pentium processor with mmx technology "
    case 15
      processtype = "celeron "
    case 16
      processtype = "pentium ii xeon "
    case 17
      processtype = "pentium iii "
    case 18
      processtype = "m1 family "
    case 19
      processtype = "m2 family "
    case 24
      processtype = "k5 family "
    case 25
      processtype = "k6 family "
    case 26
      processtype = "k6-2 "
    case 27
      processtype = "k6-3 "
    case 28
      processtype = "amd athlon processor family "
    case 29
      processtype = "amd duron processor "
    case 30
      processtype = "amd2900 family "
    case 31
      processtype = "k6-2+ "
    case 32
      processtype = "power pc family "
    case 33
      processtype = "power pc 601 "
    case 34
      processtype = "power pc 603 "
    case 35
      processtype = "power pc 603+ "
    case 36
      processtype = "power pc 604 "
    case 37
      processtype = "power pc 620 "
    case 38
      processtype = "power pc x704 "
    case 39
      processtype = "power pc 750 "
    case 48
      processtype = "alpha family "
    case 49
      processtype = "alpha 21064 "
    case 50
      processtype = "alpha 21066 "
    case 51
      processtype = "alpha 21164 "
    case 52
      processtype = "alpha 21164pc "
    case 53
      processtype = "alpha 21164a "
    case 54
      processtype = "alpha 21264 "
    case 55
      processtype = "alpha 21364 "
    case 64
      processtype = "mips family "
    case 65
      processtype = "mips r4000 "
    case 66
      processtype = "mips r4200 "
    case 67
      processtype = "mips r4400 "
    case 68
      processtype = "mips r4600 "
    case 69
      processtype = "mips r10000 "
    case 80
      processtype = "sparc family "
    case 81
      processtype = "supersparc "
    case 82
      processtype = "microsparc ii "
    case 83
      processtype = "microsparc iiep "
    case 84
      processtype = "ultrasparc "
    case 85
      processtype = "ultrasparc ii "
    case 86
      processtype = "ultrasparc iii "
    case 87
      processtype = "ultrasparc iii "
    case 88
      processtype = "ultrasparc iiii "
    case 96
      processtype = "68040 "
    case 97
      processtype = "68xxx family "
    case 98
      processtype = "68000 "
    case 99
      processtype = "68010 "
    case 100
      processtype = "68020 "
    case 101
      processtype = "68030 "
    case 112
      processtype = "hobbit family "
    case 120
      processtype = "crusoe tm5000 family "
    case 121
      processtype = "crusoe tm3000 family "
    case 128
      processtype = "weitek "
    case 130
      processtype = "itanium processor "
    case 144
      processtype = "pa-risc family "
    case 145
      processtype = "pa-risc 8500 "
    case 146
      processtype = "pa-risc 8000 "
    case 147
      processtype = "pa-risc 7300lc "
    case 148
      processtype = "pa-risc 7200 "
    case 149
      processtype = "pa-risc 7100lc "
    case 150
      processtype = "pa-risc 7100 "
    case 160
      processtype = "v30 family "
    case 176
      processtype = "pentium iii xeon "
    case 177
      processtype = "pentium iii processor with intel speedstep technology "
    case 178
      processtype = "pentium 4 "
    case 179
      processtype = "intel xeon "
    case 180
      processtype = "as400 family "
    case 181
      processtype = "intel xeon processor mp "
    case 182
      processtype = "amd athlonxp family "
    case 183
      processtype = "amd athlonmp family "
    case 184
      processtype = "intel itanium 2 "
    case 185
      processtype = "amd opteron family "
    case 190
      processtype = "k7 "
    case 200
      processtype = "ibm390 family "
    case 201
      processtype = "g4 "
    case 202
      processtype = "g5 "
    case 250
      processtype = "i860 "
    case 251
      processtype = "i960 "
    case 260
      processtype = "sh-3 "
    case 261
      processtype = "sh-4 "
    case 280
      processtype = "arm "
    case 281
      processtype = "strongarm "
    case 300
      processtype = "6x86 "
    case 301
      processtype = "mediagx "
    case 302
      processtype = "mii "
    case 320
      processtype = "winchip "
    case 350
      processtype = "dsp "
    case 500
      processtype = "video processor "
  end select
  listbox1.items.add("family: " & processtype)
end function
function cpustat(byval cpustnum)
  dim stat
  select case cpustnum
    case 0
      stat = "unknown "
    case 1
      stat = "cpu enabled "
    case 2
      stat = "cpu disabled by user via bios setup "
    case 3
      stat = "cpu disabled by bios (post error) "
    case 4
      stat = "cpu is idle "
    case 5
      stat = "reserved "
    case 6
      stat = "reserved "
    case 7
      stat = "other "
  end select
  listbox1.items.add("cpustatus: " & stat)
end function
function processortype(byval proctypenum)
  dim proctype
  select case proctypenum
    case 1
      proctype = "other "
    case 2
      proctype = "unknown "
    case 3
      proctype = "central processor "
    case 4
      proctype = "math processor "
    case 5
      proctype = "dsp processor "
    case 6
      proctype = "video processor "
  end select
  listbox1.items.add("processor type: " & proctype)
end function
end class

得到cd-rom信息


   本文相关代码如下:
imports system.management
public class form1
  inherits system.windows.forms.form
#region " windows 窗体设计器生成的代码 "
public sub new()
  mybase.new()
  '该调用是 windows 窗体设计器所必需的。
  initializecomponent()
  '在 initializecomponent() 调用之后添加任何初始化
end sub
'窗体重写 dispose 以清理组件列表。
protected overloads overrides sub dispose(byval disposing as boolean)
  if disposing then
    if not (components is nothing) then
      components.dispose()
    end if
  end if
  mybase.dispose(disposing)
end sub
'windows 窗体设计器所必需的
'注意: 以下过程是 windows 窗体设计器所必需的
'可以使用 windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
private components as system.componentmodel.icontainer
friend withevents listbox1 as system.windows.forms.listbox
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
  me.listbox1 = new system.windows.forms.listbox
  me.suspendlayout()
  '
  'listbox1
  '
  me.listbox1.location = new system.drawing.point(24, 16)
  me.listbox1.name = "listbox1"
  me.listbox1.size = new system.drawing.size(416, 173)
  me.listbox1.tabindex = 0
  '
  'form1
  '
  me.autoscalebasesize = new system.drawing.size(5, 13)
  me.clientsize = new system.drawing.size(456, 206)
  me.controls.addrange(new system.windows.forms.control() {me.listbox1})
  me.name = "form1"
  me.text = "form1"
  me.resumelayout(false)
end sub
#end region
private sub form1_load(byval sender as system.object, byval e as system.eventargs) _
    handles mybase.load
  on error resume next
  dim sounddevicequery as new selectquery("win32_cdromdrive")
  dim sounddevicesearch as new managementobjectsearcher(sounddevicequery)
  dim sounddeviceinfo as managementobject
  for each sounddeviceinfo in sounddevicesearch.get()
    dim sizeinmbs as long = (val(sounddeviceinfo("size").tostring()))
    sizeinmbs = int((sizeinmbs / (1024 * 1024)))
    listbox1.items.add("cd-rom description: " & sounddeviceinfo("caption").tostring())
    listbox1.items.add("cd-rom manufacturer: " & sounddeviceinfo("manufacturer").tostring())
    listbox1.items.add("cd-rom drive: " & sounddeviceinfo("drive").tostring())
    listbox1.items.add("cd-rom media loaded: " & sounddeviceinfo("medialoaded").tostring())
    listbox1.items.add("cd-rom media type: " & sounddeviceinfo("mediatype").tostring())
    listbox1.items.add("cd-rom volume name: " & sounddeviceinfo("volumename").tostring())
    listbox1.items.add("cd-rom size: " & sizeinmbs & " mbytes")
    listbox1.items.add("cd-rom status: " & sounddeviceinfo("status").tostring())
    listbox1.items.add("cd-rom maxmediasize: " & sounddeviceinfo("maxmediasize").tostring())
    listbox1.items.add("cd-rom id: " & sounddeviceinfo("id").tostring())
    listbox1.items.add("cd-rom transferrate: "+int(sounddeviceinfo("transferrate").tostring())+" kbs/秒")
  next
end sub
end class

得到硬盘信息


   本文相关代码如下:
imports system.management
public class form1
  inherits system.windows.forms.form
#region " windows form designer generated code "
public sub new()
  mybase.new()
  initializecomponent()
end sub
protected overloads overrides sub dispose(byval disposing as boolean)
  if disposing then
    if not (components is nothing) then
      components.dispose()
    end if
  end if
  mybase.dispose(disposing)
end sub
private components as system.componentmodel.icontainer
friend withevents listbox1 as system.windows.forms.listbox
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
  me.listbox1 = new system.windows.forms.listbox
  me.suspendlayout()
  '
  'listbox1
  '
  me.listbox1.location = new system.drawing.point(8, 8)
  me.listbox1.name = "listbox1"
  me.listbox1.size = new system.drawing.size(272, 212)
  me.listbox1.tabindex = 0
  '
  'form1
  '
  me.autoscalebasesize = new system.drawing.size(5, 13)
  me.clientsize = new system.drawing.size(292, 238)
  me.controls.addrange(new system.windows.forms.control() {me.listbox1})
  me.name = "form1"
  me.text = "form1"
  me.resumelayout(false)
end sub
#end region
private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
  on error resume next
  dim hdddevicequery as new selectquery("win32_diskdrive")
  dim hdddevicesearch as new managementobjectsearcher(hdddevicequery)
  dim hdddeviceinfo as managementobject
  for each hdddeviceinfo in hdddevicesearch.get()
    listbox1.items.add("hdd description: " & hdddeviceinfo("caption").tostring())
    listbox1.items.add("hdd bytespersector: " & hdddeviceinfo("bytespersector").tostring())
    listbox1.items.add("hdd compressionmethod: " & hdddeviceinfo("compressionmethod").tostring())
    listbox1.items.add("hdd index: " & hdddeviceinfo("index").tostring())
    listbox1.items.add("hdd installdate: " & hdddeviceinfo("installdate").tostring())
    listbox1.items.add("hdd manufacturer: " & hdddeviceinfo("manufacturer").tostring())
    listbox1.items.add("hdd partitions: " & hdddeviceinfo("partitions").tostring())
    listbox1.items.add("hdd size: " & int(val(hdddeviceinfo("size").tostring()) / 2 ^ 30) & "  gbytes")
    listbox1.items.add("hdd totalcylinders: " & hdddeviceinfo("totalcylinders").tostring())
    listbox1.items.add("hdd totalsectors: " & hdddeviceinfo("totalsectors").tostring())
    listbox1.items.add("hdd trackspercylinder: " & hdddeviceinfo("trackspercylinder").tostring())
    listbox1.items.add("hdd totalheads: " & hdddeviceinfo("totalheads").tostring())
    listbox1.items.add("hdd totaltracks: " & hdddeviceinfo("totaltracks").tostring())
    listbox1.items.add("hdd sectorspertrack: " & hdddeviceinfo("sectorspertrack").tostring())
    listbox1.items.add("hdd scsilogicalunit: " & hdddeviceinfo("scsilogicalunit").tostring())
  next
end sub
end class

 

得到声卡信息


   本文相关代码如下:
imports system.management
public class form1
  inherits system.windows.forms.form
#region " windows form designer generated code "
public sub new()
  mybase.new()
  initializecomponent()
end sub
protected overloads overrides sub dispose(byval disposing as boolean)
  if disposing then
    if not (components is nothing) then
      components.dispose()
    end if
  end if
  mybase.dispose(disposing)
end sub
private components as system.componentmodel.icontainer
friend withevents listbox1 as system.windows.forms.listbox
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
  me.listbox1 = new system.windows.forms.listbox
  me.suspendlayout()
  '
  'listbox1
  '
  me.listbox1.location = new system.drawing.point(8, 8)
  me.listbox1.name = "listbox1"
  me.listbox1.size = new system.drawing.size(272, 212)
  me.listbox1.tabindex = 0
  '
  'form1
  '
  me.autoscalebasesize = new system.drawing.size(5, 13)
  me.clientsize = new system.drawing.size(292, 238)
  me.controls.addrange(new system.windows.forms.control() {me.listbox1})
  me.name = "form1"
  me.text = "form1"
  me.resumelayout(false)
end sub
#end region
private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
  dim sounddevicequery as new selectquery("win32_sounddevice")
  dim sounddevicesearch as new managementobjectsearcher(sounddevicequery)
  dim sounddeviceinfo as managementobject
  for each sounddeviceinfo in sounddevicesearch.get()
    listbox1.items.add("sound device description: " & sounddeviceinfo("caption").tostring())
    listbox1.items.add("sound device status: " & sounddeviceinfo("status").tostring())
    listbox1.items.add("sound device manufacturer: " & sounddeviceinfo("manufacturer").tostring())
  next
end sub
end class

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表