Jump to content

reading hard disk number

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Can anyone tell me how to read the Hard Disk Serial Number as well as the volume number...

#2
mH_p0rtal

mH_p0rtal

    Newbie

  • Members
  • PipPip
  • 10 posts
hi its easy to be done in .NET
for motherboard
Public Function GetMotherboardSerialNumber() As String  Dim searcher As New System.Management.ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BaseBoard")  For Each obj As System.Management.ManagementObject In searcher.Get  Return obj.Properties("SerialNumber").Value.ToString  Next  Return String.Empty
    End Function
for hard disk
#Region " Imports "
Imports System
Imports System.IO
Imports System.Management
Imports System.Windows.Forms
#End Region

Public Class Form1
    '
#Region " Form1 "
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckHDSN()
    End Sub
    '
    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        MessageBox.Show("Hard Disk Serial Number Matching")
    End Sub
#End Region
    '
#Region " Private Sub "
    Private Sub CheckHDSN()
        Me.Cursor = Cursors.WaitCursor
        Try
            Dim Searcher_P As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PhysicalMedia")
            For Each queryObj As ManagementObject In Searcher_P.Get()
                If queryObj("SerialNumber").ToString.Trim = "Y2S0RKFE" Then
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            Next
        Catch ex As Exception
            MessageBox.Show("An error occurred while querying for WMI data: Win32_PhysicalMedia " & ex.Message)
        End Try
        '
        Try
            Dim Searcher_L As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'")
            For Each queryObj As ManagementObject In Searcher_L.Get()
                If queryObj("VolumeSerialNumber").ToString.Trim = "226C1A0B" Then
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            Next
        Catch ex As Exception
            MessageBox.Show("An error occurred while querying for WMI data: VolumeSerialNumber " & ex.Message)
        End Try
        Me.Cursor = Cursors.Default
        MessageBox.Show("Hard Disk Serial Number Not Matching !!!" & vbNewLine & vbNewLine & "Asta La Vista Baby !!!")
        End
    End Sub
#End Region
    '
End Class


#3
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
can anyone explain me that code....

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Before attempting to explain that code:

What operating system?
C or C++?
Does it have to be in a program, or are you willing to use other methods?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

WingedPanther said:

Before attempting to explain that code:

What operating system?
C or C++?
Does it have to be in a program, or are you willing to use other methods?

I would like to do a program in C... i am using linux operating system...

C or C++ both are comfortable...

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Ignore mH_portal's post, then.

From the command line: hdparm -i /dev/hda
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
mH_p0rtal

mH_p0rtal

    Newbie

  • Members
  • PipPip
  • 10 posts
it not a C++ or C Source code , its based on .NET
by the way you can use .net source under linux environment
take a look at it
codeproject.com/KB/mcpp/DriveInfoEx.aspx

#8
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

WingedPanther said:

Ignore mH_portal's post, then.

From the command line: hdparm -i /dev/hda

when i used this command... it says
/dev/hda: No such file or directory


But i got it in command line: sudo lshw -C disk | grep serial

But what about as a C program... Can't we get it using a C program... What about for windows operating system

#9
mH_p0rtal

mH_p0rtal

    Newbie

  • Members
  • PipPip
  • 10 posts
hdparm is a command line utility for the Linux and Windows OS to set and view SATA & IDE hard disk hardware parameters.
be careful about what kinda hard disk do you have ? sata or ide
if you have ide your disk drives it would be like that: hda1 hda2 hda3 under linux
or if you have SATA disk drives it would be like that: sda1 , sda2 , so on
some distribution wont mount hard drives automatically, you have to mount your drives before working with them.
i think its not necessary to write a C app for it , write a bash file to save the serial number in a Txt file.
for windows app , i gave u the source that you need to compile in .NET
hdparm for windows :
hdparm-win32.dyndns.org/hdparm/
hdparm for linux :
sourceforge.net/projects/hdparm/

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You can get it using a C program, but the question becomes: what are you trying to do with it?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
mH_p0rtal

mH_p0rtal

    Newbie

  • Members
  • PipPip
  • 10 posts
i think that its better get your hands abit mix up with Code
i can suggest a trick to use C language to gain serial number ,
you know how to retrieve serial number via terminal , now you can use system function in C , like
system("hdparm /dev/hda0/",ret_var);
then extract the serial number portion from ret_Var

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
What system() function are you using? system() only takes one argument.

system(3): execute shell command - Linux man page

You might want to look into stat(), though.

stat(2): file status - Linux man page
sudo rm -rf /