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;
            }
        }
    }
  • 相关阅读:
    Spring.profile配合Jenkins发布War包,实现开发、测试和生产环境的按需切换
    Ubuntu 配置 Tomcat
    Proper usage of Java -D command-line parameters
    Linux下设置MySql自动启动
    cent6.x配置主机名及静态网络
    vmware can not be closed virtual machine is busy
    VMware虚拟机下扩容磁盘(centos7)
    Spring、MyBatis、Shiro、Quartz、Activiti框架
    Jenkins ChangeLog
    JEECG DataGridColumn dictionary使用问题
  • 原文地址:https://www.cnblogs.com/xh831213/p/2746530.html
Copyright © 2011-2022 走看看