点滴积累,融会贯通

-----喜欢一切有兴趣的东西

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
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;
        }
    }
}
posted on 2012-10-30 16:14  小寒  阅读(1928)  评论(0编辑  收藏  举报