我有一个旧的vb应用程序,它使用此代码检索磁盘ID。
Dim FSO As New Scripting.FileSystemObject
Dim Dr As Scripting.Drive
Dim id_convertido As Long = 0
Dim sRutaAplicacion As String = Application.StartupPath
Dim Valor As Integer = sRutaAplicacion.IndexOf(":\")
If Valor <> -1 Then
Dim RaizAplicacion As String
RaizAplicacion = Mid(sRutaAplicacion, 1, Valor)
For Each Dr In FSO.Drives
If Dr.DriveLetter = RaizAplicacion Then
Dim idDisco As String
idDisco = Dr.SerialNumber
id_convertido = (Microsoft.VisualBasic.Right(idDisco, 8))
Return id_convertido
End If
Next
End If
我想在c#中实现相同的功能,所以我找到了这段代码:
string HDD = System.Environment.CurrentDirectory.Substring(0, 1);
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + HDD + ":\"");
disk.Get();
return disk["VolumeSerialNumber"].ToString();
但我得到了不同的价值观。在我的第一个代码中,“idDisco”是“876823094”,但在c#中,此值为“34434236”。
两者都在检查磁盘c:
任何线索?
非常感谢!
答案 0 :(得分:0)
检查this thread中的解决方案。
自定义类HardDrive
用于存储检索到的信息。
获取硬盘型号
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach(ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
hd.Model = wmi_HD["Model"].ToString();
hd.Type = wmi_HD["InterfaceType"].ToString();
hdCollection.Add(hd);
}
(最低)类HardDrive
public class HardDrive
{
public string Model { get; set }
public string Type { get; set; }
}
获取序列号
searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
int i = 0;
foreach(ManagementObject wmi_HD in searcher.Get())
{
// get the hard drive from collection
// using index
HardDrive hd = (HardDrive)hdCollection[i];
// get the hardware serial no.
if (wmi_HD["SerialNumber"] == null)
hd.SerialNo = "None";
else
hd.SerialNo = wmi_HD["SerialNumber"].ToString();
++i;
}