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!
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();