Source Code for Abused Altar Boys -- Getting the Name of your USB Key

I don't know where this stained glass window is, but it should be taken down.

OK, I have some C# .NET source code for getting the name of USB key. I have a program where a bunch of data is on a Cruzer USB thumb drive or USB key. I wanted to make sure that the Cruzer USB memory stick was stuck into a USB port on the computer. To test this code, I created a simple button that uses System Management to create a ManagementObjectSearcher. The searcher got a ManagementObjectCollection that I iterated through to find my Cruzer thumb drive. (To get the name of your thumb drive, just stick it in a port, and once the system recognizes it, open My Computer and look at the name.) The code is simple. You have to start with the Management directive:

using System.Management;

private void buttonUSBVerify_Click(object sender, EventArgs e)
{
ManagementObjectSearcher theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'");

ManagementObjectCollection collection = theSearcher.Get();

foreach (ManagementObject item in collection)
{
foreach (PropertyData property in item.Properties)
{
try
{
if (property.Name.Equals("Caption"))
{
if (property.Value.ToString().Contains("Cruzer -- put in the name of your"))
{
MessageBox.Show("Found it");
}
}
}
catch (Exception)
{
}
}
}
}

No comments: