Reputation: 1967
I have an old vb application which retrieves the disk id using this code.
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
I'm want to achieve the same functionality in c#, so I found this code:
string HDD = System.Environment.CurrentDirectory.Substring(0, 1);
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + HDD + ":\"");
disk.Get();
return disk["VolumeSerialNumber"].ToString();
but i get different values. In my first code, "idDisco" is "876823094", but in c#, this value is "34434236".
Both are checking disk c:
Any clues?
Thank you very much!
Upvotes: 0
Views: 931
Reputation: 942000
In my first code, "idDisco" is "876823094", but in c#, this value is "34434236"
It is the same value, 876823094 in hexadecimal notation is 0x34434236. Use Calc.exe, View + Programmer to see this for yourself. If you want to reproduce the same number that FSO gave you then you'll have to do the same thing it does and convert to base 10. Like this:
string hex = disk["VolumeSerialNumber"].ToString();
int value = int.Parse(hex, System.Globalization.NumberStyles.HexNumber, null);
string fso = value.ToString();
Upvotes: 3
Reputation: 1911
Check the solution in this thread.
The custom class HardDrive
is used to store the retrieved information.
Get Hard Drive model
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);
}
The (minimum) class HardDrive
public class HardDrive
{
public string Model { get; set }
public string Type { get; set; }
}
Get the Serial Number
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;
}
Upvotes: 0