zoukankan      html  css  js  c++  java
  • WMI判断USB设备为移动硬盘

    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(02)))
                    {
                        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;
            }
        }
    }
  • 相关阅读:
    哇塞 今天是数论专场呢 我要爆炸了
    树状数组模板题 hdu 1166
    [思维]Radar Scanner
    [思维]Minimum Spanning Tree
    [容斥]数对
    [概率]Lucky Coins
    [数学]特征方程求线性递推方程的通项公式
    [树状数组][2019徐州网络赛I]query
    [计算几何]Piece of Cake
    [欧拉降幂][2019南京网络赛B]super_log
  • 原文地址:https://www.cnblogs.com/xh831213/p/2746530.html
Copyright © 2011-2022 走看看