• 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;
            }
        }
    }
  • 相关阅读:
    【转】C#中的委托和事件
    ASP.NET上传文件实例代码
    我在配置OpenCV环境以及使用VS2013运行代码时遇到的问题
    JAVA_HOME环境变量失效的解决办法
    一道int和unsigned char之间强制类型转换的题目
    为什么不看好OpenStack:它的没落不可避免[转]
    一个销前谈云桌面的适用场景(再议云桌面营销之选择正确的客户环境)
    云桌面的前世今生
    云桌面三大谎言之GPU虚拟化
    如何理解清零心态
  • 原文地址:https://www.cnblogs.com/xh831213/p/2746530.html
Copyright © 2020-2023  润新知