介绍
再一次问好!今天我就讲讲如何通过WMI来检索硬盘信息。有时有必要知道磁盘上有多少可用空间、它是什么类型的驱动器和/或正在使用的文件系统。不是很常见,我们可能需要知道硬盘的序列号是什么;特别是当我们想让用户为我们的程序购买许可证时。我将使用 VB 和 C# 涵盖所有这些内容。让我们开始派对吧!
WMI
引用自MSDN的 WMI(Windows 管理规范)是在基于 Windows 的操作系统上管理数据和操作的基础结构。好的,通俗地说,这意味着通过使用WMI,我们可以检索硬件和/或服务核心的数据。我们今天将涵盖上述主题列表,尤其是硬盘序列号,通常不容易获得。
这个怎么运作
有两种方法可以在我们的程序中使用 WMI。我们可以编写查询(类似于 SQL / LINQ 查询),称为WQL来获取此信息,或者我们可以使用System.Management和System.Management.Instrumentation命名空间,它们内置了这些查询。这就是我们今天将使用。
设计
打开 Visual Studio 并选择VB Windows 窗体项目或C# Windows 窗体项目。给它您选择的任何名称并添加五个按钮。您的设计屏幕应类似于图 1:
图 1 –我们的设计
通过单击Project、References、System.Management将System.Management引用添加到您的项目。
命名空间
将以下命名空间添加到您的代码中:
VB.NET:
Imports System.Management
imports System.Management.Instrumentation
C# :
using System.Management;
using System.Management.Instrumentation;
获取序列号
添加下一个代码段:
VB.NET:
Public Function GetHDSerialNo(ByVal strDrive As String) As String 'Get HD Serial Number
'Ensure Valid Drive Letter Entered, Else, Default To C
If strDrive = "" OrElse strDrive Is Nothing Then
strDrive = "C"
End If
'Make Use Of Win32_LogicalDisk To Obtain Hard Disk Properties
Dim moHD As New ManagementObject("Win32_LogicalDisk.DeviceID=""" + strDrive + ":""")
'Get Info
moHD.[Get]()
'Get Serial Number
Return moHD("VolumeSerialNumber").ToString()
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Hard Disk Serial Number = " & GetHDSerialNo("C")) 'Call GetHDSerialNo Sub
End Sub
C# :
public string GetHDSerialNo(string strDrive) //Get HD Serial Number
{
//Ensure Valid Drive Letter Entered, Else, Default To C
if (string.IsNullOrEmpty(strDrive) || strDrive == null)
{
strDrive = "C";
}
//Make Use Of Win32_LogicalDisk To Obtain Hard Disk Properties
ManagementObject moHD = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + strDrive + ":\"");
//Get Info
moHD.Get();
//Get Serial Number
return moHD["VolumeSerialNumber"].ToString();
}
private void Button1_Click(System.Object sender, System.EventArgs e)
{
MessageBox.Show("Hard Disk Serial Number = " + GetHDSerialNo("C")); //Call GetHDSerialNo Sub
}
在这里,我们首先检查是否提供了有效的驱动器号,否则我们将默认为 C:\。然后我们使用Win32_LogicalDisk对象来获取特定磁盘的属性。最后,我们引用VolumeSerialNumber属性,它将为我们提供磁盘的序列号。最后,我们从 Button 中调用了这个 sub。
图 2 –硬盘序列号
获取硬盘大小
添加下一个代码。
VB.NET:
Public Function GetHDSize(ByVal strDrive As String) As Double 'Get Size of Specified Disk
'Ensure Valid Drive Letter Entered, Else, Default To C
If strDrive = "" OrElse strDrive Is Nothing Then
strDrive = "C"
End If
'Make Use Of Win32_LogicalDisk To Obtain Hard Disk Properties
Dim moHD As New ManagementObject("Win32_LogicalDisk.DeviceID=""" + strDrive + ":""")
'Get Info
moHD.[Get]()
'Get Hard Disk Size
Return Convert.ToDouble(moHD("Size"))
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim dblSize As Double 'Store Size
dblSize = Math.Round(GetHDSize("C") / 1024 / 1024 / 1024) 'Call GetHDSize Sub and Divide 3 Times By 1024 ( Byte ) To Give GB
'1 KB = 1024 - KiloByte
'1 MB = 1024 ^ 2 - MegaByte
'1 GB = 1024 ^ 3 - GigaByte
'1 TB = 1024 ^ 4 - TeraByte
'1 PB = 1024 ^ 5 - PetaByte
'1 EB = 1024 ^ 6 - ExaByte
'1 ZB = 1024 ^ 7 - ZettaByte
'1 YB = 1024 ^ 8 - YottaByte
'1 BB = 1024 ^ 9 - BrontoByte
MessageBox.Show("Hard Disk Size = " & dblSize.ToString() & " GB") 'Display Result
End Sub
C# :
public double GetHDSize(string strDrive) //Get Size of Specified Disk
{
//Ensure Valid Drive Letter Entered, Else, Default To C
if (string.IsNullOrEmpty(strDrive) || strDrive == null)
{
strDrive = "C";
}
//Make Use Of Win32_LogicalDisk To Obtain Hard Disk Properties
ManagementObject moHD = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + strDrive + ":\"");
//Get Info
moHD.Get();
//Get Hard Disk Size
return Convert.ToDouble(moHD["Size"]);
}
private void Button2_Click(System.Object sender, System.EventArgs e)
{
double dblSize = 0; //Store Size
dblSize = Math.Round(GetHDSize("C") / 1024 / 1024 / 1024); //Call GetHDSize Sub and Divide 3 Times By 1024 ( Byte ) To Give GB
//1 KB = 1024 - KiloByte
//1 MB = 1024 ^ 2 - MegaByte
//1 GB = 1024 ^ 3 - GigaByte
//1 TB = 1024 ^ 4 - TeraByte
//1 PB = 1024 ^ 5 - PetaByte
//1 EB = 1024 ^ 6 - ExaByte
//1 ZB = 1024 ^ 7 - ZettaByte
//1 YB = 1024 ^ 8 - YottaByte
//1 BB = 1024 ^ 9 - BrontoByte
MessageBox.Show("Hard Disk Size = " + dblSize.ToString() + " GB"); //Display Result
}
该函数的代码与GetHDSerialNo函数几乎完全相同;唯一的区别是我们在函数中使用了 Size 参数。请注意,所有即将到来的函数都将以相同的方式运行,因此我不会再次详细介绍这些函数中使用的方法。
结果数字不会四舍五入。它也不会显示 KB、MB、GB 甚至 TB。我们必须自己结合这种逻辑。幸运的是,这很容易!我们需要做的就是将结果 x 次数除以 1024(字节大小)。我将它划分了三遍,它给了我GigaBytes的正确结果。
很多人不知道这些尺寸的水平,这就是为什么我把它包括在这里。只是为了有趣🙂
图 3 –硬盘大小
获得可用空间
添加下一个代码。
VB.NET:
Public Function GetHDFreeSpace(ByVal strDrive As String) As Double
'Ensure Valid Drive Letter Entered, Else, Default To C
If strDrive = "" OrElse strDrive Is Nothing Then
strDrive = "C"
End If
'Make Use Of Win32_LogicalDisk To Obtain Hard Disk Properties
Dim moHD As New ManagementObject("Win32_LogicalDisk.DeviceID=""" + strDrive + ":""")
'Get Info
moHD.[Get]()
'Get Hard Disk Free Space
Return Convert.ToDouble(moHD("FreeSpace"))
End Function
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim dblFree As Double 'Store Size
dblFree = Math.Round(GetHDFreeSpace("C") / 1024 / 1024 / 1024) 'Call GetHDFreeSpace Sub and Divide 3 Times By 1024 ( Byte ) To Give GB
'1 KB = 1024 - KiloByte
'1 MB = 1024 ^ 2 - MegaByte
'1 GB = 1024 ^ 3 - GigaByte
'1 TB = 1024 ^ 4 - TeraByte
'1 PB = 1024 ^ 5 - PetaByte
'1 EB = 1024 ^ 6 - ExaByte
'1 ZB = 1024 ^ 7 - ZettaByte
'1 YB = 1024 ^ 8 - YottaByte
'1 BB = 1024 ^ 9 - BrontoByte
MessageBox.Show("Hard Disk Free Space = " & dblFree.ToString & " GB") 'Display Result
End Sub
C# :
public double GetHDFreeSpace(string strDrive)
{
//Ensure Valid Drive Letter Entered, Else, Default To C
if (string.IsNullOrEmpty(strDrive) || strDrive == null)
{
strDrive = "C";
}
//Make Use Of Win32_LogicalDisk To Obtain Hard Disk Properties
ManagementObject moHD = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + strDrive + ":\"");
//Get Info
moHD.Get();
//Get Hard Disk Free Space
return Convert.ToDouble(moHD["FreeSpace"]);
}
private void Button3_Click(System.Object sender, System.EventArgs e)
{
double dblFree = 0; //Store Size
dblFree = Math.Round(GetHDFreeSpace("C") / 1024 / 1024 / 1024); //Call GetHDFreeSpace Sub and Divide 3 Times By 1024 ( Byte ) To Give GB
//1 KB = 1024 - KiloByte
//1 MB = 1024 ^ 2 - MegaByte
//1 GB = 1024 ^ 3 - GigaByte
//1 TB = 1024 ^ 4 - TeraByte
//1 PB = 1024 ^ 5 - PetaByte
//1 EB = 1024 ^ 6 - ExaByte
//1 ZB = 1024 ^ 7 - ZettaByte
//1 YB = 1024 ^ 8 - YottaByte
//1 BB = 1024 ^ 9 - BrontoByte
MessageBox.Show("Hard Disk Free Space = " + dblFree.ToString() + " GB"); //Display Result
}
工作方式与 Size 完全相同,但我们使用FreeSpace属性。
图 4 –硬盘可用空间
获取驱动器类型
VB.NET:
Public Function GetHDDriveType(ByVal strDrive As String) As String
'Ensure Valid Drive Letter Entered, Else, Default To C
If strDrive = "" OrElse strDrive Is Nothing Then
strDrive = "C"
End If
'Make Use Of Win32_LogicalDisk To Obtain Hard Disk Properties
Dim moHD As New ManagementObject("Win32_LogicalDisk.DeviceID=""" + strDrive + ":""")
'Get Info
moHD.[Get]()
'Get Drive Type
Return moHD("DriveType").ToString()
End Function
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim strDriveType As String 'Determine Drive Type
Select Case GetHDDriveType("C")
Case "0"
strDriveType = "Unknown"
Case "1"
strDriveType = "Readable"
Case "2"
strDriveType = "Writable"
Case "3"
strDriveType = "Read / Write Supported"
Case "4"
strDriveType = "Write Once"
End Select
MessageBox.Show("Hard Disk Drive Type = " & strDriveType)
End Sub
C# :
public string GetHDDriveType(string strDrive)
{
//Ensure Valid Drive Letter Entered, Else, Default To C
if (string.IsNullOrEmpty(strDrive) || strDrive == null)
{
strDrive = "C";
}
//Make Use Of Win32_LogicalDisk To Obtain Hard Disk Properties
ManagementObject moHD = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + strDrive + ":\"");
//Get Info
moHD.Get();
//Get Drive Type
return moHD["DriveType"].ToString();
}
private void Button4_Click(System.Object sender, System.EventArgs e)
{
string strDriveType = null; //Determine Drive Type
switch (GetHDDriveType("C"))
{
case "0":
strDriveType = "Unknown";
break;
case "1":
strDriveType = "Readable";
break;
case "2":
strDriveType = "Writable";
break;
case "3":
strDriveType = "Read / Write Supported";
break;
case "4":
strDriveType = "Write Once";
break;
}
MessageBox.Show("Hard Disk Drive Type = " + strDriveType);
}
驱动器类型属性告诉我们所讨论的特定磁盘是否可写。结果将是数字,因此我们必须将该数字值转换为可以理解的值。我的硬盘返回3——支持读写。例如,如果我们使用 CD,那么它只能是可读的。
图 5 –硬盘类型
获取文件系统
VB.NET:
Public Function GetHDFileSystem(ByVal strDrive As String) As String
'Ensure Valid Drive Letter Entered, Else, Default To C
If strDrive = "" OrElse strDrive Is Nothing Then
strDrive = "C"
End If
'Make Use Of Win32_LogicalDisk To Obtain Hard Disk
Dim moHD As New ManagementObject("Win32_LogicalDisk.DeviceID=""" + strDrive + ":""")
'Get Info
moHD.[Get]()
'Get File System
Return moHD("FileSystem").ToString()
End Function
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
MessageBox.Show("Hard Disk File System = " & GetHDFileSystem("C")) 'Call FileSystem Sub
End Sub
C# :
public string GetHDFileSystem(string strDrive)
{
//Ensure Valid Drive Letter Entered, Else, Default To C
if (string.IsNullOrEmpty(strDrive) || strDrive == null)
{
strDrive = "C";
}
//Make Use Of Win32_LogicalDisk To Obtain Hard Disk Properties
ManagementObject moHD = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + strDrive + ":\"");
//Get Info
moHD.Get();
//Get File System
return moHD["FileSystem"].ToString();
}
private void Button5_Click(System.Object sender, System.EventArgs e)
{
MessageBox.Show("Hard Disk File System = " + GetHDFileSystem("C")); //Call GetHDFileSystem
}
那里没有惊喜🙂
图 6 –硬盘文件系统
结论
显然,这只是冰山一角。您可以使用 WMI 实现更多目标。现在,您有责任确保您深入了解 WMI 的世界。我在下面附上两个项目的压缩格式,以防您错过了一两步。我希望你喜欢这篇文章。直到下一次,干杯!