using KGIS.Framework.Utils; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Management; using System.Text; using System.Windows.Forms; namespace Kingo.PluginServiceInterface { public class USBFlashDiskHelper { private Timer Timer { get; set; } //public List ListUSBInfo { get; set; } //private ManagementClass mgtCls { get; set; } public List lstUSB { get; set; } public Action, string> LoadUSBInfo; private bool IsFirst = true; public USBFlashDiskHelper() { //ListUSBInfo = new List(); lstUSB = new List(); //Timer = new Timer(); //Timer.Interval = 5000; //Timer.Tick += Timer_Tick; //Timer.Start(); } public void Stop() { try { if (this.Timer != null) { this.Timer.Stop(); } } catch (Exception ex) { LogAPI.Debug(ex); } } private void Timer_Tick(object sender, EventArgs e) { try { Timer.Stop(); lstUSB.Clear(); LoadUSB(); //var resp = ListUSBInfo.EqualList(lstUSB); ////如果和上次获取到的U盘不一致,或者是第一次加载,则通知获取到的U盘信息 //if (!resp || IsFirst) //{ // IsFirst = false; // ListUSBInfo.Clear(); // ListUSBInfo.AddRange(lstUSB); // if (LoadUSBInfo != null) // { // LoadUSBInfo(ListUSBInfo, string.Empty); // } //} } catch (Exception ex) { if (LoadUSBInfo != null) { LoadUSBInfo(null, ex.Message); } } finally { Timer.Start(); } } public void LoadUSB() { try { lstUSB.Clear(); //获取USB移动U盘 DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { if (d.DriveType == DriveType.Removable) { lstUSB.Add(d.Name + " (" + d.VolumeLabel + ")"); } } allDrives = null; //获取移动硬盘 //if (mgtCls == null) //{ // mgtCls = new ManagementClass("Win32_DiskDrive"); //} using (ManagementClass mgtCls = new ManagementClass("Win32_DiskDrive")) { ManagementObjectCollection mos = mgtCls.GetInstances(); foreach (ManagementObject mo in mos) { if (mo.Properties["MediaType"].Value == null || mo.Properties["MediaType"].Value.ToString() != "External hard disk media") { mo.Dispose(); continue; } using (var diskPartitions = mo.GetRelated("Win32_DiskPartition")) { foreach (ManagementObject diskPartition in diskPartitions) { using (var disks = diskPartition.GetRelated("Win32_LogicalDisk")) { foreach (ManagementBaseObject disk in disks) { lstUSB.Add(disk.Properties["Name"].Value.ToString() + "\\" + " (" + disk.Properties["VolumeName"].Value.ToString() + ":" + Math.Round(double.Parse(disk.Properties["Size"].Value.ToString()) / 1024 / 1024 / 1024.00, 2) + "GB)"); //lstUSB.Add(disk.Properties["Name"].Value.ToString() + "\\" + "(" + disk["Description"] + ")"); } } } } } } } catch// (Exception ex) { //LogAPI.Debug("读取U盘异常:" + ex.Message); } } } /// /// 判断List集合是否相等扩展方法 /// public static class ListCompareExtendMethd { /// /// 判断两个字典是否是相等的(所有的字典项对应的值都相等) /// /// 字典项类型 /// 字典值类型 /// 源字典 /// 目标字典 /// 两个字典相等则返回True,否则返回False public static bool EqualDictionary(this Dictionary sourceDictionary, Dictionary targetDictionary) where TKey : IEquatable where TValue : IEquatable { //空字典直接返回False,即使是两个都是空字典,也返回False if (sourceDictionary == null || targetDictionary == null) { return false; } if (object.ReferenceEquals(sourceDictionary, targetDictionary)) { return true; } if (sourceDictionary.Count == targetDictionary.Count && targetDictionary.Count == 0) { return true; } if (sourceDictionary.Count != targetDictionary.Count) { return false; } //比较两个字典的Key与Value foreach (var item in sourceDictionary) { //如果目标字典不包含源字典任意一项,则不相等 if (!targetDictionary.ContainsKey(item.Key)) { return false; } //如果同一个字典项的值不相等,则不相等 if (!targetDictionary[item.Key].Equals(item.Value)) { return false; } } return true; } /// /// 判断两个集合是否是相等的(所有的元素及数量都相等) /// /// 集合元素类型 /// 源集合列表 /// 目标集合列表 /// 两个集合相等则返回True,否则返回False public static bool EqualList(this IList sourceCollection, IList targetCollection) where T : IEquatable { //空集合直接返回False,即使是两个都是空集合,也返回False if (sourceCollection == null || targetCollection == null) { return false; } if (object.ReferenceEquals(sourceCollection, targetCollection)) { return true; } if (sourceCollection.Count == targetCollection.Count && targetCollection.Count == 0) { return true; } if (sourceCollection.Count != targetCollection.Count) { return false; } var sourceCollectionStaticsDict = sourceCollection.StatisticRepetition(); var targetCollectionStaticsDict = targetCollection.StatisticRepetition(); return sourceCollectionStaticsDict.EqualDictionary(targetCollectionStaticsDict); } /// /// 统计集合的重复项,并返回一个字典 /// /// 集合元素类型 /// 统计集合列表 /// 返回一个集合元素及重复数量的字典 private static Dictionary StatisticRepetition(this IEnumerable sourceCollection) where T : IEquatable { var collectionStaticsDict = new Dictionary(); foreach (var item in sourceCollection) { if (collectionStaticsDict.ContainsKey(item)) { collectionStaticsDict[item]++; } else { collectionStaticsDict.Add(item, 1); } } return collectionStaticsDict; } } /// /// 调用示例 /// public class TestGetUSBInfo { public void GetUSBInfo() { USBFlashDiskHelper uSBFlashDiskHelper = new USBFlashDiskHelper(); uSBFlashDiskHelper.LoadUSBInfo = LoadUSB; } /// /// 接收USB插拔监听返回事件 /// /// 获取插入的U盘盘符列表 /// 获取USB信息异常错误 private void LoadUSB(List lstUSBInfo, string errorMessage) { try { if (!string.IsNullOrWhiteSpace(errorMessage)) { MessageBox.Show("获取U盘异常:" + errorMessage); return; } if (lstUSBInfo == null || lstUSBInfo.Count <= 0) { MessageBox.Show("未获取到插入U盘!"); return; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }