C# .NET - How to Get HardDisk Serial Number

Asked By Lokesh Phanindra Bonthu on 04-Nov-08 01:28 PM

any one pls tell me

how can we get Harddisk Serial number with out using external dll files


HDD's serial number

san san replied to Lokesh Phanindra Bonthu on 04-Nov-08 02:23 PM
Hi

Add a reference to the System.Management Namespace.
Pass a drive letter to the method and it will return the serial number in string format. If a drive letter isn't provided it will default to the C drive.

Code snippet

//Namespace Reference

using System.Management

/// <summary>
/// method to retrieve the selected HDD's serial number
/// </summary>
/// <param name="strDriveLetter">Drive letter to retrieve serial number for</param>
/// <returns>the HDD's serial number</returns>

public string GetHDDSerialNumber(string drive)
{

    //check to see if the user provided a drive letter
    //if not default it to "C"

    if (drive == "" || drive == null)
    {
        drive = "C";
    }

    //create our ManagementObject, passing it the drive letter to the
    //DevideID using WQL

    ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive +":\"");
    //bind our management object
    disk.Get();
    //return the serial number
    return disk["VolumeSerialNumber"].ToString();
}

re

Web Star replied to Lokesh Phanindra Bonthu on 04-Nov-08 11:46 PM
using System;
using System.Collections;
using System.Management;

namespace HardDriveSample1
{
class HardDrive
{
private string model = null;
private string type = null;
private string serialNo = null;
public string Model
{
get {return model;}
set {model = value;}
}

public string Type
{
get {return type;}
set {type = value;}
}

public string SerialNo
{
get {return serialNo;}
set {serialNo = value;}
}
}

class TestProgram
{
[STAThread]
static void Main(string[] args)
{
ArrayList hdCollection = new ArrayList();
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach(ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
hdCollection.Add(hd);
}
searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
int i = 0;
foreach(ManagementObject wmi_HD in searcher.Get())
{
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;
}

foreach(HardDrive hd in hdCollection)
{
Console.WriteLine("Model\t\t: " + hd.Model);
Console.WriteLine("Type\t\t: " + hd.Type);
Console.WriteLine("Serial No.\t: " + hd.SerialNo);
Console.WriteLine();
}

Console.WriteLine("Press [Enter] to exit...");
Console.ReadLine();
}
}
}

re

Web Star replied to Lokesh Phanindra Bonthu on 05-Nov-08 12:00 AM

Use the GetVolumeInformation API function.


Code Snippet

Private Sub Command1_Click()

    Dim root As String

    Dim volume_name As String

    Dim serial_number As Long

    Dim max_component_length As Long

    Dim file_system_flags As Long

    Dim file_system_name As String

    Dim pos As Integer

 

    root = Text1.Text

    volume_name = Space$(1024)

    file_system_name = Space$(1024)

 

    If GetVolumeInformation(root, volume_name, _

        Len(volume_name), serial_number, _

        max_component_length, file_system_flags, _

        file_system_name, Len(file_system_name)) = 0 _

    Then

        MsgBox("Error getting volume information.")

        Exit Sub

    End If

 

    pos = InStr(volume_name, Chr$(0))

    volume_name = Left$(volume_name, pos - 1)

    lblVolumeName.Caption = volume_name

 

    lblSerialNumber.Caption = Format$(serial_number)

 

    lblMaxComponentLength.Caption = _

        Format$(max_component_length)

 

    pos = InStr(file_system_name, Chr$(0))

    file_system_name = Left$(file_system_name, pos - 1)

    lblFileSystem.Caption = file_system_name

 

    lblFlags.Caption = "&&H" & Hex$(file_system_flags)

End Sub

How to Get HardDisk Serial Number
Jignesh Shah replied to Lokesh Phanindra Bonthu on 05-Nov-08 12:14 AM
GET HARDDISK SERIAL NO
C_A P replied to Lokesh Phanindra Bonthu on 05-Nov-08 04:34 AM
 'Note: This code requires a referecne to the Microsoft Scripting Runtime library (SCRRUN.DLL)

Private Sub Form_Load()

'Show drive serial number for the current drive
MsgBox " Drive serial number for " & Left(App.Path, 1) & ": " & GetDriveSerialNumber
End

End Sub


Public Function
GetDriveSerialNumber(Optional ByVal DriveLetter As String) As Long

Dim
fso As Object, Drv As Object

'Create a FileSystemObject object
Set fso = CreateObject("Scripting.FileSystemObject")

'Assign the current drive letter if not specified
If DriveLetter <> "" Then
Set
Drv = fso.GetDrive(DriveLetter)
Else
Set
Drv = fso.GetDrive(fso.GetDriveName(App.Path))
End If

With
Drv
If .IsReady Then
DriveSerial = Abs(.SerialNumber)
Else '"Drive Not Ready!"
DriveSerial = -1
End If
End With

'Clean up
Set Drv = Nothing
Set fso = Nothing

GetDriveSerialNumber = DriveSerial

End Function
GET HARDDISK SERIAL NO
C_A P replied to Lokesh Phanindra Bonthu on 05-Nov-08 04:34 AM
'Simply call the following API in a Module
'-----------------------------------------
Public Declare Function GetVolumeSerialNumber Lib "kernel32"
Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal
lpVolumeNameBuffer As Long, ByVal nVolumeNameSize As Long,
lpVolumeSerialNumber As Long, ByVal lpMaximumComponentLength As Long,
ByVal lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As Long,
ByVal nFileSystemNameSize As Long) As Long

Public Function VolumeSerial(DriveLetter) As Long
Dim Serial As Long
Call GetVolumeSerialNumber(UCase(DriveLetter) & ":\", 0&, 0&,
Serial, 0&, 0&, 0&, 0&)
VolumeSerial = Serial
End Function

'Then put following command on the main form
'-------------------------------------------
MsgBox VolumeSerial("D")
Reply
Binny ch replied to Lokesh Phanindra Bonthu on 05-Nov-08 01:08 PM
Hi,
You can use FileSystemObject to get your required info.
Private Sub Command4_Click()
Dim fso As New FileSystemObject
Dim mydrive As Drive
Dim path As String
'Initialize path.
path = "C:\"
'Get object.
Set mydrive = fso.GetDrive(path)
'Check for success.
MsgBox mydrive.DriveLetter 'displays "C"
End Sub
See http://support.microsoft.com/kb/q186118/ for details.

Alternatively you can use HardDriveInfo.DLL. It is an ActiveX COM DLL to read or get hard disk information. You can use HardDriveInfo.DLL to read the real serial number of hard disk easily. HardDriveInfo.DLL does not depend on the "support" libraries to get or fetch the details. Each Hard Disks have Unique Serial Number, some time it is necessary to get the hard disk serial number. So HardDriveInfo.DLL helps us to get the Hard disk serial number easily and efficiently, you can use this serial number to create an machine id or encrypt number.

The call to this dll as below:

Declare Function GetHDDSerialId Lib "HardDiskInfoV1.dll" () As String
Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long

Public function SetHardDiskID()as string
dim gHardDiskId as string
Dim flNumber As Long = LoadLibrary ("HardDiskInfoV1.dll")
gHardDiskId = GetHDDSerialId

If Asc(Right(gHardDiskId, 1)) = 0 Then
gHardDiskId = Trim(Mid(gHardDiskId, 1, Len(gHardDiskId) - 1))
Else
gHardDiskId = Trim(gHardDiskId)
End If
return gHardDiskId

see http://www.diskserialnumber.com/ and http://addressof.com/blog/archive/2004/02/13/386.aspx for details.
Manoj replied to Web Star on 28-Aug-10 12:28 AM
thanks for ur code

but how to run this code in iis


--
regards,
manoj singh