WMI determines that the USB device is a mobile hard disk

Source: Internet
Author: User
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;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.