C# .NET - Get HardDisk Serial number in all windows

Asked By Dalia ff on 24-Jul-05 04:05 PM
Please if anyone knows how to Get HardDisk Serial number in any windows 
either by coding or adding Alibrary please help me

Sample code - Asked By Peter Bromberg on 24-Jul-05 04:41 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();
		}
	}
}

copy code - carlos dunga replied to Peter Bromberg on 02-Jul-09 11:31 PM

Peter Bromberg  the code you posted here was copied from code project , you should give credit to the owner of the code http://www.codeproject.com/KB/cs/hard_disk_serialno.aspx

cdunga

These lins of code can be found in 1180 search ressults from google.

Peter Bromberg replied to carlos dunga on 03-Jul-09 08:27 AM
However, if the above was actually obtained from Codeproject, I'm happy to say so.