How to find the serial number of a hard disk device without using WMI in .NET?

I want to get the hard number from the hard drive, but DO NOT use WMI. I tried using WMI code and it does not work on my machine. So, is there an alternative in .NET for finding the serial number of a physical hard drive?

+4
c # wmi hard-drive
source share
9 answers

This should help start:

How to get the serial number of a physical hard drive without WMI

Regarding your problem with WMI not returning data; Are you sure how to get data from WMI? You can use WMI Tools to check / fix this.

+4
source share

Rough and ready:

using System; using System.Management; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ManagementClass mangnmt = new ManagementClass("Win32_LogicalDisk"); ManagementObjectCollection mcol = mangnmt.GetInstances(); string result = ""; foreach (ManagementObject strt in mcol) { result += "Name : " + Convert.ToString(strt["Name"]) + Environment.NewLine; result += "VolumeName : " + Convert.ToString(strt["VolumeName"]) + Environment.NewLine; result += "VolumeSerialNumber: " + Convert.ToString(strt["VolumeSerialNumber"]) + Environment.NewLine; result += Environment.NewLine; } Console.Out.WriteLine(result); Console.In.ReadLine(); } } } 

Please note that there are other attributes that you can use, and if you need details for a particular drive, you will need to do some processing of the results.

Hope this helps!

+3
source share

A perfectly working solution can be found here:

http://www.codeproject.com/Articles/16941/Get-Physical-HDD-Serial-Number-without-WMI

[edit] Sorry, missed the link to this already sent.

+1
source share

Use createfile as shown below. This may require administrative permissions. Add 4 text fields and a button to your code.

  private const int CREATE_NEW = 1; private const int OPEN_EXISTING = 3; private const uint GENERIC_READ = 0x80000000; private const uint GENERIC_WRITE = 0x40000000; // 10000000000000000000000000000000 --- GENERIC_READ // 01000000000000000000000000000000 --- GENERIC_WRITE // 00100000000000000000000000000000 --- GENERIC_EXECUTE // 00010000000000000000000000000000 --- GENERIC_ALL //Securable objects use an access mask format in which the //four high-order bits specify generic access rights private const int FILE_SHARE_READ = 0x1; private const int FILE_SHARE_WRITE = 0x2; // 00000000000000000000000000000001 --- FILE_SHARE_READ // 00000000000000000000000000000010 --- FILE_SHARE_WRITE // 00000000000000000000000000000100 --- FILE_SHARE_DELETE private const int VER_PLATFORM_WIN32_NT = 2; private const int DFP_RECEIVE_DRIVE_DATA = 0x7C088; // 0 000000000000111 11 0 00000100010 00 // Common Device Type Required Access Custom Function Code Transfer Type private const int INVALID_HANDLE_VALUE = -1; public enum DriveTypes { Fixed, Removable, Unknown }; public string[] DriveStrings = { "Fixed", "Removable", "Unknown" }; [StructLayout(LayoutKind.Sequential, Size = 8)] private class IDEREGS { public byte Features; public byte SectorCount; public byte SectorNumber; public byte CylinderLow; public byte CylinderHigh; public byte DriveHead; public byte Command; public byte Reserved; } [StructLayout(LayoutKind.Sequential, Size = 32)] private class SENDCMDINPARAMS { public int BufferSize; public IDEREGS DriveRegs; public byte DriveNumber; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] public byte[] Reserved; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public int[] Reserved2; public SENDCMDINPARAMS() { DriveRegs = new IDEREGS(); Reserved = new byte[3]; Reserved2 = new int[4]; } } [StructLayout(LayoutKind.Sequential, Size = 12)] private class DRIVERSTATUS { public byte DriveError; public byte IDEStatus; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public byte[] Reserved; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public int[] Reserved2; public DRIVERSTATUS() { Reserved = new byte[2]; Reserved2 = new int[2]; } } [StructLayout(LayoutKind.Sequential)] private class IDSECTOR { public short GenConfig; public short NumberCylinders; public short Reserved; public short NumberHeads; public short BytesPerTrack; public short BytesPerSector; public short SectorsPerTrack; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] public short[] VendorUnique; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] public char[] SerialNumber; public short BufferClass; public short BufferSize; public short ECCSize; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public char[] FirmwareRevision; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)] public char[] ModelNumber; public short MoreVendorUnique; public short DoubleWordIO; public short Capabilities; public short Reserved1; public short PIOTiming; public short DMATiming; public short BS; public short NumberCurrentCyls; public short NumberCurrentHeads; public short NumberCurrentSectorsPerTrack; public int CurrentSectorCapacity; public short MultipleSectorCapacity; public short MultipleSectorStuff; public int TotalAddressableSectors; public short SingleWordDMA; public short MultiWordDMA; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 382)] public byte[] Reserved2; public IDSECTOR() { VendorUnique = new short[3]; Reserved2 = new byte[382]; FirmwareRevision = new char[8]; SerialNumber = new char[20]; ModelNumber = new char[40]; } } [StructLayout(LayoutKind.Sequential)] private class SENDCMDOUTPARAMS { public int BufferSize; public DRIVERSTATUS Status; public IDSECTOR IDS; public SENDCMDOUTPARAMS() { Status = new DRIVERSTATUS(); IDS = new IDSECTOR(); } } [System.Runtime.InteropServices.DllImport("kernel32.dll")] private static extern int CloseHandle(int hObject); [System.Runtime.InteropServices.DllImport("kernel32.dll")] private static extern int CreateFile( string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile ); [System.Runtime.InteropServices.DllImport("kernel32.dll")] private static extern int DeviceIoControl( int hDevice, int dwIoControlCode, [In(), Out()] SENDCMDINPARAMS lpInBuffer, int lpInBufferSize, [In(), Out()] SENDCMDOUTPARAMS lpOutBuffer, int lpOutBufferSize, ref int lpBytesReturned, int lpOverlapped ); private string SwapChars(char[] chars) { for (int i = 0; i <= chars.Length - 2; i += 2) { char t; t = chars[i]; chars[i] = chars[i + 1]; chars[i + 1] = t; } string s = new string(chars); return s; } private void button1_Click(object sender, System.EventArgs e) { string serialNumber = " ", model = " ", firmware = " "; bool result; DriveTypes driveType = DriveTypes.Unknown; int handle, returnSize = 0; int driveNumber = 0; SENDCMDINPARAMS sci = new SENDCMDINPARAMS(); SENDCMDOUTPARAMS sco = new SENDCMDOUTPARAMS(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) // \\.\PhysicalDrive0 Opens the first physical drive. // \\.\PhysicalDrive2 Opens the third physical drive. // see more info on http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx handle = CreateFile("\\\\.\\PhysicalDrive" + "0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); else // for win'9x handle = CreateFile("\\\\.\\Smartvsd", 0, 0, 0, CREATE_NEW, 0, 0); if (handle != INVALID_HANDLE_VALUE) { sci.DriveNumber = (byte)driveNumber; sci.BufferSize = Marshal.SizeOf(sco); sci.DriveRegs.DriveHead = (byte)(0xA0 | driveNumber << 4); sci.DriveRegs.Command = 0xEC; sci.DriveRegs.SectorCount = 1; sci.DriveRegs.SectorNumber = 1; if (DeviceIoControl(handle, DFP_RECEIVE_DRIVE_DATA, sci, Marshal.SizeOf(sci), sco, Marshal.SizeOf(sco), ref returnSize, 0) != 0) { serialNumber = SwapChars(sco.IDS.SerialNumber); model = SwapChars(sco.IDS.ModelNumber); firmware = SwapChars(sco.IDS.FirmwareRevision); } textBox1.Text = serialNumber; textBox2.Text = model; textBox3.Text = firmware; if ((sco.IDS.GenConfig & 0x80) == 0x40) driveType = DriveTypes.Removable; else if ((sco.IDS.GenConfig & 0x40) == 0x40) driveType = DriveTypes.Fixed; else driveType = DriveTypes.Unknown; CloseHandle(handle); textBox4.Text = DriveStrings[(int)driveType]; } } 
+1
source share

You can use:

GetVolumeInformation

Win32 API function to get this information if you should avoid WMI. The linked page gives the full signature of the declaration (for both VB and C #) for the API function along with sample code.

0
source share

You want to use WMI for this , an example is provided in this article .

0
source share

I use the hdd firmware track in my projects. I programmed the mdi form behind to find the hdd firmware number, then its cycle on all the provided hdds of company computers, and if it matches any of these provided hdds firmware numbers, then run applicationatoin and download the mdi form, the message "Application is not registered on this on your computer, please call Mr. Afridi to register this application at 00923339176357. My email address is munawar_s_afridi@yahoo.com I will send the full source code for information on how to program the firmware number hdd and how to stop others using your app illegally.

One great thing that you should immediately point out to all the hdd firmware of the company computer, and let the application first select the hdd firmware number, save it in a variable and then select the value of this variable (string value) and loop it one by one with each hdd number using logical operator OR. If he finds a suitable number among the hdds of a company with a variable value, then the application should download the main form (mdi), another wise to notify the user about registration.

Sample code will be provided using vb6. You can easily program later on vb.net by simply understanding how to invoke unmanaged code into a managed .net application. In this case .dll.

0
source share

Your best options are windows api,

I did a simple search and got it

General processor information as well as read this post

0
source share

Use WMI from Server Explorer to VS

  • Open Server Explorer
  • Turn the car around
  • Expand Management Classes
  • Expand Disk Volumes
  • Select drive
  • Drag it to the form to see the generated code
  • Access the VolumeSerialNumber property in code
-one
source share

All Articles