using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
namespace WMITest
{
class Program
{
static void Main(string[] args)
{
var usBdriveNames = GetAllUsbDriveNames();
CheckAllDriveType(usBdriveNames);
Console.ReadLine();
}
private static void CheckAllDriveType(List<string> usBdriveNames)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
bool isUsb = false;
if (usBdriveNames.Contains(d.Name.Substring(0, 2)))
{
Console.WriteLine(d.Name + " is a USB Drive");
isUsb = true;
}
switch (d.DriveType)
{
case DriveType.Unknown:
Console.WriteLine(d.Name + " is Unknown Drive");
break;
case DriveType.NoRootDirectory:
Console.WriteLine(d.Name + " is NoRootDirectoryDrive");
break;
case DriveType.Removable:
Console.WriteLine(d.Name + " is Removable Drive");
break;
case DriveType.Fixed:
if (isUsb)
{
Console.WriteLine(d.Name + " is mobile HDD"); //this is the result which we want to get
}
else
{
Console.WriteLine(d.Name + " is Fixed Drive");
}
break;
case DriveType.Network:
Console.WriteLine(d.Name + " is Network Drive");
break;
case DriveType.CDRom:
Console.WriteLine(d.Name + " is CDRom Drive");
break;
case DriveType.Ram:
Console.WriteLine(d.Name + " is Ram Drive");
break;
}
Console.WriteLine("***************************************************");
}
}
private static List<string> GetAllUsbDriveNames()
{
var searcher = new ManagementObjectSearcher();
searcher.Query = new SelectQuery("Win32_DiskDrive", "InterfaceType = \"USB\"");
var usbDiskDrives = searcher.Get().Cast<ManagementObject>();
var usBdriveNames = new List<string>();
foreach (var usbDiskDrive in usbDiskDrives)
{
searcher.Query = new SelectQuery("Win32_DiskDriveToDiskPartition");
var diskDriveToDiskPartition = searcher.Get().Cast<ManagementObject>();
searcher.Query = new SelectQuery("Win32_LogicalDiskToPartition");
var logicalDiskToPartition = searcher.Get().Cast<ManagementObject>();
searcher.Query = new SelectQuery("Win32_LogicalDisk");
var logicalDisk = searcher.Get().Cast<ManagementObject>();
var usbPartition =
diskDriveToDiskPartition.First(p => p["Antecedent"].ToString() == usbDiskDrive.ToString())[
"Dependent"].ToString();
var usbLogicalDisk =
logicalDiskToPartition.First(p => p["Antecedent"].ToString() == usbPartition)["Dependent"].ToString();
foreach (ManagementObject disk in logicalDisk)
{
if (disk.ToString() == usbLogicalDisk)
{
usBdriveNames.Add(disk["DeviceID"].ToString());
}
}
}
return usBdriveNames;
}
}
}
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
namespace WMITest
{
class Program
{
static void Main(string[] args)
{
var usBdriveNames = GetAllUsbDriveNames();
CheckAllDriveType(usBdriveNames);
Console.ReadLine();
}
private static void CheckAllDriveType(List<string> usBdriveNames)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
bool isUsb = false;
if (usBdriveNames.Contains(d.Name.Substring(0, 2)))
{
Console.WriteLine(d.Name + " is a USB Drive");
isUsb = true;
}
switch (d.DriveType)
{
case DriveType.Unknown:
Console.WriteLine(d.Name + " is Unknown Drive");
break;
case DriveType.NoRootDirectory:
Console.WriteLine(d.Name + " is NoRootDirectoryDrive");
break;
case DriveType.Removable:
Console.WriteLine(d.Name + " is Removable Drive");
break;
case DriveType.Fixed:
if (isUsb)
{
Console.WriteLine(d.Name + " is mobile HDD"); //this is the result which we want to get
}
else
{
Console.WriteLine(d.Name + " is Fixed Drive");
}
break;
case DriveType.Network:
Console.WriteLine(d.Name + " is Network Drive");
break;
case DriveType.CDRom:
Console.WriteLine(d.Name + " is CDRom Drive");
break;
case DriveType.Ram:
Console.WriteLine(d.Name + " is Ram Drive");
break;
}
Console.WriteLine("***************************************************");
}
}
private static List<string> GetAllUsbDriveNames()
{
var searcher = new ManagementObjectSearcher();
searcher.Query = new SelectQuery("Win32_DiskDrive", "InterfaceType = \"USB\"");
var usbDiskDrives = searcher.Get().Cast<ManagementObject>();
var usBdriveNames = new List<string>();
foreach (var usbDiskDrive in usbDiskDrives)
{
searcher.Query = new SelectQuery("Win32_DiskDriveToDiskPartition");
var diskDriveToDiskPartition = searcher.Get().Cast<ManagementObject>();
searcher.Query = new SelectQuery("Win32_LogicalDiskToPartition");
var logicalDiskToPartition = searcher.Get().Cast<ManagementObject>();
searcher.Query = new SelectQuery("Win32_LogicalDisk");
var logicalDisk = searcher.Get().Cast<ManagementObject>();
var usbPartition =
diskDriveToDiskPartition.First(p => p["Antecedent"].ToString() == usbDiskDrive.ToString())[
"Dependent"].ToString();
var usbLogicalDisk =
logicalDiskToPartition.First(p => p["Antecedent"].ToString() == usbPartition)["Dependent"].ToString();
foreach (ManagementObject disk in logicalDisk)
{
if (disk.ToString() == usbLogicalDisk)
{
usBdriveNames.Add(disk["DeviceID"].ToString());
}
}
}
return usBdriveNames;
}
}
}