void P_Add(string str_Input)
{
txt_new_Temp.Text += Environment.NewLine + str_Input;
}
private void button2_Click(object sender, EventArgs e)
{
ManagementObjectSearcher diskDrives = new ManagementObjectSearcher(“SELECT * FROM Win32_DiskDrive WHERE InterfaceType=’USB'”);
foreach (ManagementObject diskDrive in diskDrives.Get())
{
string DeviceID = diskDrive[“DeviceID”].ToString();
string DriveLetter = “”;
string DriveDescription = “”;
// associate physical disks with partitions
ManagementObjectSearcher partitionSearcher = new ManagementObjectSearcher(String.Format(
“associators of {{Win32_DiskDrive.DeviceID='{0}’}} where AssocClass = Win32_DiskDriveToDiskPartition”,
diskDrive[“DeviceID”]));
foreach (ManagementObject partition in partitionSearcher.Get())
{
// associate partitions with logical disks (drive letter volumes)
ManagementObjectSearcher logicalSearcher = new ManagementObjectSearcher(String.Format(
“associators of {{Win32_DiskPartition.DeviceID='{0}’}} where AssocClass = Win32_LogicalDiskToPartition”,
partition[“DeviceID”]));
foreach (ManagementObject logical in logicalSearcher.Get())
{
// finally find the logical disk entry to determine the volume name
ManagementObjectSearcher volumeSearcher = new ManagementObjectSearcher(String.Format(
“select * from Win32_LogicalDisk where Name='{0}'”,
logical[“Name”]));
foreach (ManagementObject volume in volumeSearcher.Get())
{
DriveLetter = volume[“Name”].ToString();
if (volume[“VolumeName”] != null)
DriveDescription = volume[“VolumeName”].ToString();
char VolumeLetter = DriveLetter[0];
string VolumeName = DriveDescription;
string Manufacturer = (string)diskDrive[“Manufacturer”];
string MediaType = (string)diskDrive[“MediaType”];
string Model = (string)diskDrive[“Model”];
string SerialNumber = (string)diskDrive[“SerialNumber”];
long Size = Convert.ToInt64(volume[“Size”]);
long FreeSpace = Convert.ToInt64(volume[“FreeSpace”]);
P_Add(“—————–“);
P_Add(“VolumeLetter= ” + VolumeLetter.ToString());
P_Add(“VolumeName= ” + VolumeName);
P_Add(“Manufacturer= ” + Manufacturer);
P_Add(“MediaType= ” + MediaType);
P_Add(“Model= ” + Model);
P_Add(“SerialNumber= ” + SerialNumber);
P_Add(“Size= ” + Size.ToString());
P_Add(“FreeSpace= ” + FreeSpace.ToString());
P_Add(” “);
}
}
}
}
}