//通过hardwareid和friendlyname找出节点 private HardwareInfo SearchNodeByHardwareId(string DeviceHardwareId, string DeviceFriendlyName, IList <HardwareInfo> xsList) { HardwareInfo hInfo = new HardwareInfo(); int i; for (i = 0; i < xsList.Count; i++) { if (DeviceHardwareId.ToLower().Contains(xsList[i].HardwareId.ToLower()) && DeviceFriendlyName.ToLower().Contains(xsList[i].DeviceName.ToLower())) { Console.WriteLine("Found: " + DeviceFriendlyName); hInfo = xsList[i]; break; } } //return true; return(hInfo); }
public static IList<HardwareInfo> GetHardwareTable(out Dictionary<Guid, HardwareInfo> ParentDic) { IList<HardwareInfo> _ReturnList = new List<HardwareInfo>(); ParentDic = new Dictionary<Guid, HardwareInfo>(); Guid _NewGuid = Guid.Empty; IntPtr _MainIntPtr = Externs.SetupDiGetClassDevs(ref _NewGuid, 0, IntPtr.Zero, Externs.DIGCF_ALLCLASSES | Externs.DIGCF_PRESENT); if (_MainIntPtr.ToInt32() == -1) { return _ReturnList; } Externs.SP_DEVINFO_DATA _DevinfoData; _DevinfoData = new Externs.SP_DEVINFO_DATA(); _DevinfoData.classGuid = System.Guid.Empty; _DevinfoData.cbSize = 28; _DevinfoData.devInst = 0; _DevinfoData.reserved = 0; StringBuilder _DeviceName = new StringBuilder(""); _DeviceName.Capacity = 1000; Int32 dwRequireSize = 0; uint i = 0; uint j = 0; while (Externs.SetupDiEnumDeviceInfo(_MainIntPtr, i, _DevinfoData)) { Externs.SetupDiGetDeviceRegistryProperty(_MainIntPtr, _DevinfoData, Externs.SPDRP_DEVICEDESC, 0, _DeviceName, (uint)_DeviceName.Capacity, IntPtr.Zero); _ReturnList.Add(new HardwareInfo(_DeviceName.ToString(), _DevinfoData.classGuid, _DevinfoData.cbSize, _DevinfoData.devInst, _DevinfoData.reserved)); //_ReturnList.Add(_DeviceName.ToString()); Console.WriteLine("子节点:" + _DeviceName.ToString()); if (!Externs.SetupDiGetClassDescription(ref _DevinfoData.classGuid, _DeviceName, 260, ref dwRequireSize)) { i++; continue; }; //往字典里边添加父节点 HardwareInfo hardwareInfo = new HardwareInfo(_DeviceName.ToString(), _DevinfoData.classGuid, _DevinfoData.cbSize, _DevinfoData.devInst, _DevinfoData.reserved); if (!ParentDic.ContainsKey(hardwareInfo.ClassGuid)) { ParentDic.Add(hardwareInfo.ClassGuid, hardwareInfo); Console.WriteLine("设备Guid:{0}已加入字典\n ", ParentDic[hardwareInfo.ClassGuid].DeviceName); } //foreach (KeyValuePair<string, string> kvp in openWith) //{ // Console.WriteLine("Key = {0}, Value = {1}", // kvp.Key, kvp.Value); //} //给父节点添加新节点 Console.WriteLine("父节点:" + _DeviceName + (++j).ToString()); Console.WriteLine("===================="); i++; } Console.WriteLine("\n\n\n"); foreach (KeyValuePair<Guid, HardwareInfo> kvp in ParentDic) { Console.WriteLine("Guid = {0}, 设备类型 = {1}", kvp.Key, kvp.Value.DeviceName); } return _ReturnList; }
public static IList <HardwareInfo> GetHardwareTable(out Dictionary <Guid, HardwareInfo> ParentDic) { IList <HardwareInfo> _ReturnList = new List <HardwareInfo>(); ParentDic = new Dictionary <Guid, HardwareInfo>(); Guid _NewGuid = Guid.Empty; IntPtr _MainIntPtr = Externs.SetupDiGetClassDevs(ref _NewGuid, 0, IntPtr.Zero, Externs.DIGCF_ALLCLASSES | Externs.DIGCF_PRESENT); //if (_MainIntPtr.ToInt32() == -1) //{ // return _ReturnList; //} // if (_MainIntPtr.ToInt64() == Externs.INVALID_HANDLE_VALUE) { throw new Exception("Invalid Handle"); } // Externs.SP_DEVINFO_DATA _DevinfoData; _DevinfoData = new Externs.SP_DEVINFO_DATA(); _DevinfoData.classGuid = System.Guid.Empty; //for 32-bit, IntPtr.Size = 4 //for 64-bit, IntPtr.Size = 8 if (IntPtr.Size == 4) { _DevinfoData.cbSize = 28; } else if (IntPtr.Size == 8) { _DevinfoData.cbSize = 32; } //_DevinfoData.cbSize =28; _DevinfoData.devInst = 0; _DevinfoData.reserved = 0; StringBuilder _DeviceName = new StringBuilder(""); StringBuilder DeviceHardwareId = new StringBuilder(""); UInt32 status, problem; string dstatustr = ""; Externs.DeviceStatus deviceStatus = Externs.DeviceStatus.Unknown; _DeviceName.Capacity = 1000; DeviceHardwareId.Capacity = 1000; Int32 dwRequireSize = 0; uint i = 0; uint j = 0; while (Externs.SetupDiEnumDeviceInfo(_MainIntPtr, i, _DevinfoData)) { if (Externs.SetupDiGetDeviceRegistryProperty(_MainIntPtr, _DevinfoData, Externs.SPDRP_FRIENDLYNAME, 0, _DeviceName, (uint)_DeviceName.Capacity, IntPtr.Zero)) { //状态 if (Externs.CM_Get_DevNode_Status(out status, out problem, i, 0) == Externs.CR_SUCCESS) { deviceStatus = ((status & Externs.DN_DISABLEABLE) > 0) ? Externs.DeviceStatus.Enabled : Externs.DeviceStatus.Disabled; } //设备的DeviceHardwareId Externs.SetupDiGetDeviceRegistryProperty(_MainIntPtr, _DevinfoData, Externs.SPDRP_HARDWAREID, 0, DeviceHardwareId, Externs.MAX_DEV_LEN, IntPtr.Zero); _ReturnList.Add(new HardwareInfo(_DeviceName.ToString(), _DevinfoData.classGuid, _DevinfoData.cbSize, _DevinfoData.devInst, _DevinfoData.reserved, DeviceHardwareId.ToString(), dstatustr, deviceStatus)); } else { Externs.SetupDiGetDeviceRegistryProperty(_MainIntPtr, _DevinfoData, Externs.SPDRP_DEVICEDESC, 0, _DeviceName, (uint)_DeviceName.Capacity, IntPtr.Zero); _ReturnList.Add(new HardwareInfo(_DeviceName.ToString(), _DevinfoData.classGuid, _DevinfoData.cbSize, _DevinfoData.devInst, _DevinfoData.reserved, DeviceHardwareId.ToString(), dstatustr, deviceStatus)); } //_ReturnList.Add(_DeviceName.ToString()); Console.WriteLine("子节点:" + _DeviceName.ToString()); if (!Externs.SetupDiGetClassDescription(ref _DevinfoData.classGuid, _DeviceName, 260, ref dwRequireSize)) { i++; continue; } ; //往字典里边添加父节点 HardwareInfo hardwareInfo = new HardwareInfo(_DeviceName.ToString(), _DevinfoData.classGuid, _DevinfoData.cbSize, _DevinfoData.devInst, _DevinfoData.reserved, DeviceHardwareId.ToString(), dstatustr, deviceStatus); if (!ParentDic.ContainsKey(hardwareInfo.ClassGuid)) { ParentDic.Add(hardwareInfo.ClassGuid, hardwareInfo); Console.WriteLine("设备Guid:{0}已加入字典\n ", ParentDic[hardwareInfo.ClassGuid].DeviceName); } //foreach (KeyValuePair<string, string> kvp in openWith) //{ // Console.WriteLine("Key = {0}, Value = {1}", // kvp.Key, kvp.Value); //} //给父节点添加新节点 Console.WriteLine("父节点:" + _DeviceName + (++j).ToString()); Console.WriteLine("===================="); i++; } Console.WriteLine("\n\n\n"); foreach (KeyValuePair <Guid, HardwareInfo> kvp in ParentDic) { Console.WriteLine("Guid = {0}, 设备类型 = {1}", kvp.Key, kvp.Value.DeviceName); } return(_ReturnList); }