Using System;
Using System. Collections. Generic;
Using System. IO;
Using System. LINQ;
Using System. Management;
NamespaceWmitest
{
ClassProgram
{
Static VoidMain (String[] ARGs)
{
VaRUsbdrivenames = 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 ();
Searcher. query =NewSelectquery ("Win32_logicaldisk");
VaRLogicaldisk = 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;
}
}
}