At first it may seems it is very easy question and some body may be trying to give me advice to try Google, it may be so. But for me it is very hard I have try Google, Stack Overflow and can’t find any good solution.
Just want to get Serial number of Hard Disk or Hard Drive using C#
Please read carefully: serial number of Hard Disk, but not Serial number of Volume of Hard Disk (e.g. C, D, E, etc).
For getting serial no of volume of hard disk I have found solution on net and its work well but problem is with Getting serial number of Hard Disk.
Some body may trying to make this question as possible copy of below Stake Overflow question or may suggest link of that question. But it is not
And not any below question provides good solution for this problem in C#:
This is the final solution:
Get Physical HDD Serial Number without WMI
write this much code:
DriveListEx diskInfo = new DriveListEx();
diskInfo.Load();
string serialNo = diskInfo[0].SerialNumber;
Don't forgot to add reference to the DriveInfoEx.dll
.
see this
http://www.codeproject.com/KB/system/GetHardwareInformation.aspx
just download demo from there and select "data storage" tab and select Win32_DiskDrive from this you will get information all the Disk drives(HardDisk) mention below and see one property "SerialNumber" after sectorpertrack and before signature property...
The best way I found is:
Download the .dll from here
Add the .dll to your project
Add this code:
[DllImportAttribute("HardwareIDExtractorC.dll")]
public static extern String GetIDESerialNumber(byte DriveNumber);
Call the hard disk ID from where you need it:
GetIDESerialNumber(0).Replace(" ", string.Empty);
Note: Go to the properties of the dll in explorer and set Build Action
to Embedded Resource
.
// Function driveser (model)
// Returns the serial number of the drive specified in "model" or an empty string.
// Please include this is you are going to use it.
// (C) By Zibri 2013
// Free for non commercial use.
// zibri AT zibri DOT org
public string driveser(string model)
{
string functionReturnValue = null;
string devid = "";
functionReturnValue = "";
try {
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskDrive WHERE Model LIKE '%" + model + "%'");
foreach (ManagementObject queryObj in searcher.Get()) {
if (!string.IsNullOrEmpty(queryObj("SerialNumber")))
functionReturnValue = queryObj("SerialNumber");
Debug.Print(queryObj("Model") + ":" + functionReturnValue);
}
} catch (ManagementException err) {
Debug.Print("An error occurred while querying for WMI data: " + err.Message);
}
return functionReturnValue;
}
I took a look with ILSpy (http://ilspy.net/) to System.IO.DriveInfo class and I figured out this code that seems to work fine :
'------------------------------------------------------
' Declaration found in Microsoft.Win32.Win32Native
'------------------------------------------------------
Friend Declare Auto Function GetVolumeInformation Lib "kernel32.dll" (drive As String, <Out()> volumeName As StringBuilder, volumeNameBufLen As Integer, <Out()> ByRef volSerialNumber As Integer, <Out()> ByRef maxFileNameLen As Integer, <Out()> ByRef fileSystemFlags As Integer, <Out()> fileSystemName As StringBuilder, fileSystemNameBufLen As Integer) As Boolean
'------------------------------------------------------
' Test in my Form class
'------------------------------------------------------
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
Try
Dim volumeName As StringBuilder = New StringBuilder(50)
Dim stringBuilder As StringBuilder = New StringBuilder(50)
Dim volSerialNumber As Integer
Dim maxFileNameLen As Integer
Dim fileSystemFlags As Integer
If Not GetVolumeInformation("C:\", volumeName, 50, volSerialNumber, maxFileNameLen, fileSystemFlags, stringBuilder, 50) Then
Dim lastWin32Error As Integer = Marshal.GetLastWin32Error()
MsgBox("Error number:" & lastWin32Error)
Else
MsgBox(volSerialNumber.ToString("X"))
End If
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
来源:https://stackoverflow.com/questions/5675761/serial-number-of-hard-disk-or-hard-drive