vb中,可直接利用代码获得硬件信息吗?若可以,请作示范,谢谢!
2个回答
展开全部
完全可以,VB可以使用Windows管理设备(WMI)进行读取计算机硬件信息。
Dim s, System, item
Dim i As Integer
Set System = GetObject("winmgmts:").InstancesOf("Win32_ComputerSystem")
For Each item In System
‘List1.AddItem item.cputype
s = "Computer Info" & vbCrLf
s = s & "***********************" & vbCrLf
s = s & "计算机名称: " & item.name & vbCrLf
s = s & "状态: " & item.Status & vbCrLf
s = s & "类型: " & item.SystemType & vbCrLf
s = s & "生产厂家: " & item.Manufacturer & vbCrLf
s = s & "型号: " & item.Model & vbCrLf
s = s & "内存: ~" & item.totalPhysicalMemory \ 1024000 & "mb" & vbCrLf
s = s & "域: " & item.domain & vbCrLf
‘s = s & "工作组" & item.Workgroup & vbCrLf ‘获得工作组和域的选项不能同时用
s = s & "当前用户: " & item.username & vbCrLf
s = s & "启动状态" & item.BootupState & vbCrLf
s = s & "该计算机属于" & item.PrimaryOwnerName & vbCrLf
s = s & "系统类型" & item.CreationClassName & vbCrLf
s = s & "计算机类类型" & item.Description & vbCrLf
For i = 0 To 1 ‘这里假设安装了两个系统
s = s & Chr(5) & "启动选项" & i & " :" & item.SystemStartupOptions(i) _
& vbCrLf
Next i
Next
Text1.Text = s
运行上面的代码后,在textBox中就可以显示计算机的信息。
在上面的代码中,程序通过GetObject("winmgmts:")获得WMI对象,然后获得下面的
Win32_ComputerSystem子项并通过访问Win32_ComputerSystem对象中的分项获得系统中的信息。
*需要说明的是,并不是所有的系统都支持WMI,在有些系统中无法显示生产厂家等信息。
Dim s, System, item
Dim i As Integer
Set System = GetObject("winmgmts:").InstancesOf("Win32_ComputerSystem")
For Each item In System
‘List1.AddItem item.cputype
s = "Computer Info" & vbCrLf
s = s & "***********************" & vbCrLf
s = s & "计算机名称: " & item.name & vbCrLf
s = s & "状态: " & item.Status & vbCrLf
s = s & "类型: " & item.SystemType & vbCrLf
s = s & "生产厂家: " & item.Manufacturer & vbCrLf
s = s & "型号: " & item.Model & vbCrLf
s = s & "内存: ~" & item.totalPhysicalMemory \ 1024000 & "mb" & vbCrLf
s = s & "域: " & item.domain & vbCrLf
‘s = s & "工作组" & item.Workgroup & vbCrLf ‘获得工作组和域的选项不能同时用
s = s & "当前用户: " & item.username & vbCrLf
s = s & "启动状态" & item.BootupState & vbCrLf
s = s & "该计算机属于" & item.PrimaryOwnerName & vbCrLf
s = s & "系统类型" & item.CreationClassName & vbCrLf
s = s & "计算机类类型" & item.Description & vbCrLf
For i = 0 To 1 ‘这里假设安装了两个系统
s = s & Chr(5) & "启动选项" & i & " :" & item.SystemStartupOptions(i) _
& vbCrLf
Next i
Next
Text1.Text = s
运行上面的代码后,在textBox中就可以显示计算机的信息。
在上面的代码中,程序通过GetObject("winmgmts:")获得WMI对象,然后获得下面的
Win32_ComputerSystem子项并通过访问Win32_ComputerSystem对象中的分项获得系统中的信息。
*需要说明的是,并不是所有的系统都支持WMI,在有些系统中无法显示生产厂家等信息。
展开全部
Public Function CheckComm()'调用此过程
On Error Resume Next
Set wshshell = CreateObject("wscript.shell")
wshshell.run ("%comspec% /c net start winmgmt"), 0
Set WshNetwork = CreateObject("WScript.Network")
computername = WshNetwork.computername
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'主板
Set board = objWMIService.ExecQuery("select * from win32_baseboard")
For Each Item In board
board2 = Item.Product
Next
'CPU
Set cpu = objWMIService.ExecQuery("select * from win32_processor")
For Each Item In cpu
cpu2 = Item.Name
Next
'内存
Set colItems = objWMIService.ExecQuery("Select * from Win32_PhysicalMemory", , 48)
Dim memory1
For Each objItem In colItems
a = objItem.capacity / 1048576
temp = temp + Val(objItem.capacity)
If InStr(memory1, "1条" & a & "M") <> 0 Then
memory1 = Replace(memory1, "1条" & a & "M", "2条" & a & "M")
ElseIf InStr(memory1, "2条" & a & "M") <> 0 Then
memory1 = Replace(memory1, "2条" & a & "M", "3条" & a & "M")
ElseIf InStr(memory1, "3条" & a & "M") <> 0 Then
memory1 = Replace(memory1, "3条" & a & "M", "4条" & a & "M")
Else
memory1 = memory1 & "1条" & a & "M"
End If
n = n + 1
Next
memory = temp / 1048576
If n = 1 Then
memory2 = memory1
Else
memory2 = memory1 & " 总计" & memory & "M"
End If
'硬盘
Set disk = objWMIService.ExecQuery("select * from win32_diskdrive")
For Each Item In disk
disk2 = Item.Model
Next
'显卡
Set video = objWMIService.ExecQuery("select * from win32_videocontroller", , 48)
For Each Item In video
video2 = Item.Description
Next
'网卡
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter", , 48)
For Each objItem In colItems
If (Left(objItem.NetConnectionID, 4) = "本地连接") Then
lanname = objItem.Name
End If
Next
lan2 = lanname
Dim thecomputer(6) '将结果保存到数组中
thecomputer(0) = board2
thecomputer(1) = cpu2
thecomputer(2) = memory2
thecomputer(3) = disk2
thecomputer(4) = video2
thecomputer(5) = lan2
CheckComm = thecomputer
End Function
追问
用不了啊,总提示语法错误
追答
看附件吧 做好2个版本的文件。不管你用vb.net 也好 还是vb6 都可以直接使用
~用电脑来下载
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询