You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
277 lines
10 KiB
277 lines
10 KiB
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<string> ListUSBInfo { get; set; } |
|
//private ManagementClass mgtCls { get; set; } |
|
public List<string> lstUSB { get; set; } |
|
|
|
public Action<List<string>, string> LoadUSBInfo; |
|
|
|
private bool IsFirst = true; |
|
public USBFlashDiskHelper() |
|
{ |
|
//ListUSBInfo = new List<string>(); |
|
lstUSB = new List<string>(); |
|
//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); |
|
} |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 判断List集合是否相等扩展方法 |
|
/// </summary> |
|
public static class ListCompareExtendMethd |
|
{ |
|
/// <summary> |
|
/// 判断两个字典是否是相等的(所有的字典项对应的值都相等) |
|
/// </summary> |
|
/// <typeparam name="TKey">字典项类型</typeparam> |
|
/// <typeparam name="TValue">字典值类型</typeparam> |
|
/// <param name="sourceDictionary">源字典</param> |
|
/// <param name="targetDictionary">目标字典</param> |
|
/// <returns>两个字典相等则返回True,否则返回False</returns> |
|
public static bool EqualDictionary<TKey, TValue>(this Dictionary<TKey, TValue> sourceDictionary, Dictionary<TKey, TValue> targetDictionary) |
|
where TKey : IEquatable<TKey> |
|
where TValue : IEquatable<TValue> |
|
{ |
|
//空字典直接返回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; |
|
} |
|
|
|
/// <summary> |
|
/// 判断两个集合是否是相等的(所有的元素及数量都相等) |
|
/// </summary> |
|
/// <typeparam name="T">集合元素类型</typeparam> |
|
/// <param name="sourceCollection">源集合列表</param> |
|
/// <param name="targetCollection">目标集合列表</param> |
|
/// <returns>两个集合相等则返回True,否则返回False</returns> |
|
public static bool EqualList<T>(this IList<T> sourceCollection, IList<T> targetCollection) where T : IEquatable<T> |
|
{ |
|
//空集合直接返回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); |
|
} |
|
|
|
/// <summary> |
|
/// 统计集合的重复项,并返回一个字典 |
|
/// </summary> |
|
/// <typeparam name="T">集合元素类型</typeparam> |
|
/// <param name="sourceCollection">统计集合列表</param> |
|
/// <returns>返回一个集合元素及重复数量的字典</returns> |
|
private static Dictionary<T, int> StatisticRepetition<T>(this IEnumerable<T> sourceCollection) where T : IEquatable<T> |
|
{ |
|
var collectionStaticsDict = new Dictionary<T, int>(); |
|
foreach (var item in sourceCollection) |
|
{ |
|
if (collectionStaticsDict.ContainsKey(item)) |
|
{ |
|
collectionStaticsDict[item]++; |
|
} |
|
else |
|
{ |
|
collectionStaticsDict.Add(item, 1); |
|
} |
|
} |
|
return collectionStaticsDict; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 调用示例 |
|
/// </summary> |
|
public class TestGetUSBInfo |
|
{ |
|
public void GetUSBInfo() |
|
{ |
|
USBFlashDiskHelper uSBFlashDiskHelper = new USBFlashDiskHelper(); |
|
uSBFlashDiskHelper.LoadUSBInfo = LoadUSB; |
|
} |
|
|
|
/// <summary> |
|
/// 接收USB插拔监听返回事件 |
|
/// </summary> |
|
/// <param name="lst">获取插入的U盘盘符列表</param> |
|
/// <param name="errorMessage">获取USB信息异常错误</param> |
|
private void LoadUSB(List<string> 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); |
|
} |
|
} |
|
} |
|
}
|
|
|