在.NET中得到计算机硬件信息的一些功能
得到显示器分辨率
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)
得到计算机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
1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
2Me.ListBox1 = New System.Windows.Forms.ListBox
3Me.Button1 = New System.Windows.Forms.Button
4Me.SuspendLayout()
5'
6'ListBox1
7'
8Me.ListBox1.Location = New System.Drawing.Point(8, 8)
9Me.ListBox1.Name = "ListBox1"
10Me.ListBox1.Size = New System.Drawing.Size(280, 186)
11Me.ListBox1.TabIndex = 0
12'
13'Button1
14'
15Me.Button1.Location = New System.Drawing.Point(56, 208)
16Me.Button1.Name = "Button1"
17Me.Button1.Size = New System.Drawing.Size(168, 32)
18Me.Button1.TabIndex = 1
19Me.Button1.Text = "装载计算机处理器信息"
20'
21'Form1
22'
23Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
24Me.ClientSize = New System.Drawing.Size(296, 254)
25Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.ListBox1})
26Me.Text = "计算机处理器信息"
27Me.ResumeLayout(False)
28
29End Sub
30
31#End Region
32
33Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
34Handles Button1.Click
35
36Dim ProcQuery As New SelectQuery("Win32_Processor")
37Dim ProcSearch As New ManagementObjectSearcher(ProcQuery)
38Dim ProcInfo As ManagementObject
39
40For Each ProcInfo In ProcSearch.Get()
41Call processorfamily(ProcInfo("Family").ToString)
42ListBox1.Items.Add("Description: " & ProcInfo("Description").ToString())
43ListBox1.Items.Add("caption: " & ProcInfo("caption").ToString())
44ListBox1.Items.Add("Architecture: " & ProcInfo("Architecture").ToString())
45Call processortype(ProcInfo("ProcessorType").ToString())
46Call CpuStat(ProcInfo("CpuStatus").ToString)
47ListBox1.Items.Add("MaxClockSpeed: " & ProcInfo("MaxClockSpeed").ToString() & "MHZ")
48ListBox1.Items.Add("L2CacheSpeed: " & ProcInfo("L2CacheSpeed").ToString() & "MHZ")
49ListBox1.Items.Add("ExtClock: " & ProcInfo("L2CacheSpeed").ToString() & "MHZ")
50ListBox1.Items.Add("ProcessorId: " & ProcInfo("ProcessorId").ToString())
51ListBox1.Items.Add("AddressWidth: " & ProcInfo("AddressWidth").ToString() & "Bits")
52ListBox1.Items.Add("DataWidth: " & ProcInfo("DataWidth").ToString() & "Bits")
53ListBox1.Items.Add("Version: " & ProcInfo("Version").ToString())
54ListBox1.Items.Add("ExtClock: " & ProcInfo("ExtClock").ToString() & "MHZ")
55Next
56End Sub
57Function processorfamily(ByVal procssfam)
58Dim processtype
59Select Case procssfam
60Case 1
61processtype = "Other"
62Case 2
63processtype = "Unknown "
64Case 3
65processtype = "8086 "
66Case 4
67processtype = "80286 "
68Case 5
69processtype = "80386 "
70Case 6
71processtype = "80486 "
72Case 7
73processtype = "8087 "
74Case 8
75processtype = "80287 "
76Case 9
77processtype = "80387 "
78Case 10
79processtype = "80487 "
80Case 11
81processtype = "Pentium brand "
82Case 12
83processtype = "Pentium Pro "
84Case 13
85processtype = "Pentium II "
86Case 14
87processtype = "Pentium processor with MMX technology "
88Case 15
89processtype = "Celeron "
90Case 16
91processtype = "Pentium II Xeon "
92Case 17
93processtype = "Pentium III "
94Case 18
95processtype = "M1 Family "
96Case 19
97processtype = "M2 Family "
98Case 24
99processtype = "K5 Family "
100Case 25
101processtype = "K6 Family "
102Case 26
103processtype = "K6-2 "
104Case 27
105processtype = "K6-3 "
106Case 28
107processtype = "AMD Athlon Processor Family "
108Case 29
109processtype = "AMD Duron Processor "
110Case 30
111processtype = "AMD2900 Family "
112Case 31
113processtype = "K6-2+ "
114Case 32
115processtype = "Power PC Family "
116Case 33
117processtype = "Power PC 601 "
118Case 34
119processtype = "Power PC 603 "
120Case 35
121processtype = "Power PC 603+ "
122Case 36
123processtype = "Power PC 604 "
124Case 37
125processtype = "Power PC 620 "
126Case 38
127processtype = "Power PC X704 "
128Case 39
129processtype = "Power PC 750 "
130Case 48
131processtype = "Alpha Family "
132Case 49
133processtype = "Alpha 21064 "
134Case 50
135processtype = "Alpha 21066 "
136Case 51
137processtype = "Alpha 21164 "
138Case 52
139processtype = "Alpha 21164PC "
140Case 53
141processtype = "Alpha 21164a "
142Case 54
143processtype = "Alpha 21264 "
144Case 55
145processtype = "Alpha 21364 "
146Case 64
147processtype = "MIPS Family "
148Case 65
149processtype = "MIPS R4000 "
150Case 66
151processtype = "MIPS R4200 "
152Case 67
153processtype = "MIPS R4400 "
154Case 68
155processtype = "MIPS R4600 "
156Case 69
157processtype = "MIPS R10000 "
158Case 80
159processtype = "SPARC Family "
160Case 81
161processtype = "SuperSPARC "
162Case 82
163processtype = "microSPARC II "
164Case 83
165processtype = "microSPARC IIep "
166Case 84
167processtype = "UltraSPARC "
168Case 85
169processtype = "UltraSPARC II "
170Case 86
171processtype = "UltraSPARC IIi "
172Case 87
173processtype = "UltraSPARC III "
174Case 88
175processtype = "UltraSPARC IIIi "
176Case 96
177processtype = "68040 "
178Case 97
179processtype = "68xxx Family "
180Case 98
181processtype = "68000 "
182Case 99
183processtype = "68010 "
184Case 100
185processtype = "68020 "
186Case 101
187processtype = "68030 "
188Case 112
189processtype = "Hobbit Family "
190Case 120
191processtype = "Crusoe TM5000 Family "
192Case 121
193processtype = "Crusoe TM3000 Family "
194Case 128
195processtype = "Weitek "
196Case 130
197processtype = "Itanium Processor "
198Case 144
199processtype = "PA-RISC Family "
200Case 145
201processtype = "PA-RISC 8500 "
202Case 146
203processtype = "PA-RISC 8000 "
204Case 147
205processtype = "PA-RISC 7300LC "
206Case 148
207processtype = "PA-RISC 7200 "
208Case 149
209processtype = "PA-RISC 7100LC "
210Case 150
211processtype = "PA-RISC 7100 "
212Case 160
213processtype = "V30 Family "
214Case 176
215processtype = "Pentium III Xeon "
216Case 177
217processtype = "Pentium III Processor with Intel SpeedStep Technology "
218Case 178
219processtype = "Pentium 4 "
220Case 179
221processtype = "Intel Xeon "
222Case 180
223processtype = "AS400 Family "
224Case 181
225processtype = "Intel Xeon processor MP "
226Case 182
227processtype = "AMD AthlonXP Family "
228Case 183
229processtype = "AMD AthlonMP Family "
230Case 184
231processtype = "Intel Itanium 2 "
232Case 185
233processtype = "AMD Opteron Family "
234Case 190
235processtype = "K7 "
236Case 200
237processtype = "IBM390 Family "
238Case 201
239processtype = "G4 "
240Case 202
241processtype = "G5 "
242Case 250
243processtype = "i860 "
244Case 251
245processtype = "i960 "
246Case 260
247processtype = "SH-3 "
248Case 261
249processtype = "SH-4 "
250Case 280
251processtype = "ARM "
252Case 281
253processtype = "StrongARM "
254Case 300
255processtype = "6x86 "
256Case 301
257processtype = "MediaGX "
258Case 302
259processtype = "MII "
260Case 320
261processtype = "WinChip "
262Case 350
263processtype = "DSP "
264Case 500
265processtype = "Video Processor "
266End Select
267ListBox1.Items.Add("Family: " & processtype)
268
269End Function
270Function CpuStat(ByVal CpuStNUM)
271Dim stat
272Select Case CpuStNUM
273Case 0
274stat = "Unknown "
275Case 1
276stat = "CPU Enabled "
277Case 2
278stat = "CPU Disabled by User via BIOS Setup "
279Case 3
280stat = "CPU Disabled By BIOS (POST Error) "
281Case 4
282stat = "CPU is Idle "
283Case 5
284stat = "Reserved "
285Case 6
286stat = "Reserved "
287Case 7
288stat = "Other "
289End Select
290ListBox1.Items.Add("CpuStatus: " & stat)
291End Function
292Function processortype(ByVal proctypenum)
293Dim proctype
294Select Case proctypenum
295Case 1
296proctype = "Other "
297Case 2
298proctype = "Unknown "
299Case 3
300proctype = "Central Processor "
301Case 4
302proctype = "Math Processor "
303Case 5
304proctype = "DSP Processor "
305Case 6
306proctype = "Video Processor "
307End Select
308ListBox1.Items.Add("Processor Type: " & proctype)
309
310End Function
311End Class</system.diagnostics.debuggerstepthrough()>