C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 340,138 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 3,869 people online right now. Registration is fast and FREE... Join Now!




Use WMI to get HDD's serial number

This is a snippet that can be used to retrieve the serial number of a HDD

Submitted By: PsychoCoder
Actions:
Rating:
Views: 9,440

Language: C#

Last Modified: February 10, 2008
Instructions: 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

Snippet


  1. //Namespace Reference
  2. using System.Management
  3.  
  4. /// <summary>
  5. /// method to retrieve the selected HDD's serial number
  6. /// </summary>
  7. /// <param name="strDriveLetter">Drive letter to retrieve serial number for</param>
  8. /// <returns>the HDD's serial number</returns>
  9. public string GetHDDSerialNumber(string drive)
  10. {
  11.     //check to see if the user provided a drive letter
  12.     //if not default it to "C"
  13.     if (drive == "" || drive == null)
  14.     {
  15.         drive = "C";
  16.     }
  17.     //create our ManagementObject, passing it the drive letter to the
  18.     //DevideID using WQL
  19.     ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive +":\"");
  20.     //bind our management object
  21.     disk.Get();
  22.     //return the serial number
  23.     return disk["VolumeSerialNumber"].ToString();
  24. }

Copy & Paste


Comments

hungtdao 2008-09-10 08:21:46

Great example! It's simple and clear


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month