本文汇集了在.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) '更多请参见 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 "
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management ;namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 104);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(240, 23);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 134);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(240, 23);
this.label2.TabIndex = 2;
this.label2.Text = "label2";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 164);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(240, 23);
this.label3.TabIndex = 3;
this.label3.Text = "label3";
//
// label4
//
this.label4.Location = new System.Drawing.Point(24, 194);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(240, 23);
this.label4.TabIndex = 4;
this.label4.Text = "label4";
//
// label5
//
this.label5.Location = new System.Drawing.Point(24, 224);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(240, 23);
this.label5.TabIndex = 5;
this.label5.Text = "label5";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
ManagementObjectSearcher my = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
foreach(ManagementObject share in my.Get())
{
label1.Text= "主板制造商:" + share["Manufacturer"].ToString();
label2.Text= "产品:" + share["Product"].ToString();
label3.Text= "主板序列号:" + share["SerialNumber"].ToString();
}
//得到cpu序列号
string strCPUNo = "";
ManagementClass cimObject = new ManagementClass("Win32_Processor");
ManagementObjectCollection mocHard = cimObject.GetInstances();
foreach(ManagementObject moHard in mocHard)
{
strCPUNo = moHard.Properties["ProcessorId"].Value.ToString();
}
label4.Text = "CPU序列号为:" + strCPUNo;
//硬盘序列号
ManagementObject m_objDisk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
string strSN = (string)m_objDisk.GetPropertyValue("VolumeSerialNumber");
label5.Text = "硬盘序列号为:" + strSN ; }
}
}
String HDid;
ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].value;
MessageBox.Show(HDid );
}
获取CPU序列号代码
string cpuInfo = "";//cpu序列号
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].value.ToString();
MessageBox.Show(cpuInfo);
}
Form1.cs(11): 类型或命名空间名称“Management”在类或命名空间“System”中不存在(是否缺少程序集引用?)
在项目菜单--〉添加引用--〉浏览
*********************************************************************
String HDid;
ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].Value;
MessageBox.Show(HDid );
}
********************************************************************
错误地方:ManagementObjectCollection moc = cimobject.GetInstances();
提示错误:异常详细信息: System.Management.ManagementException: 访问遭到拒绝
谁说获取不到啊
作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年7月25日 5点30分58秒
--------------------------------------------------------------------------------
本文汇集了在.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)
'更多请参见
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
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 "