using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Hardware
{
public class HardwareClass
{
属性#region 属性
/// <summary>
/// 返回所有硬件信息列表
/// </summary>
/// <returns></returns>
public string[] List
{
get
{
List<string> HWList = new List<string>();
try
{
Guid myGUID = System.Guid.Empty;
IntPtr hDevInfo = Externs.SetupDiGetClassDevs(ref myGUID, 0, IntPtr.Zero, Externs.DIGCF_ALLCLASSES | Externs.DIGCF_PRESENT);
if (hDevInfo.ToInt32() == Externs.INVALID_HANDLE_VALUE)
{
throw new Exception("Invalid Handle");
}
Externs.SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData = new Externs.SP_DEVINFO_DATA();
DeviceInfoData.cbSize = 28;
DeviceInfoData.devInst = 0;
DeviceInfoData.classGuid = System.Guid.Empty;
DeviceInfoData.reserved = 0;
UInt32 i;
StringBuilder DeviceName = new StringBuilder("");
DeviceName.Capacity = Externs.MAX_DEV_LEN;
for (i = 0; Externs.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++)
{
while (!Externs.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Externs.SPDRP_DEVICEDESC, 0, DeviceName, Externs.MAX_DEV_LEN, IntPtr.Zero))
{
//Skip
}
HWList.Add(DeviceName.ToString());
}
Externs.SetupDiDestroyDeviceInfoList(hDevInfo);
}
catch (Exception ex)
...{
throw new Exception("枚举设备列表出错", ex);
}
return HWList.ToArray();
}
}
#endregion
公共事件#region 公共事件
/// <summary>
/// 清理非托管资源
/// </summary>
/// <param name="callback"></param>
public void Dispose(IntPtr callback)
{
try
{
Externs.UnregisterDeviceNotification(callback);
}
catch
{
}
}
/// <summary>
/// 设置指定设备的状态
/// </summary>
/// <param name="match">设备名称</param>
/// <param name="bEnable">是否启用</param>
/// <returns></returns>
public bool SetState(string[] match, bool bEnable)
{
try
{
Guid myGUID = System.Guid.Empty;
IntPtr hDevInfo = Externs.SetupDiGetClassDevs(ref myGUID, 0, IntPtr.Zero, Externs.DIGCF_ALLCLASSES | Externs.DIGCF_PRESENT);
if (hDevInfo.ToInt32() == Externs.INVALID_HANDLE_VALUE)
{
return false;
}
Externs.SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData = new Externs.SP_DEVINFO_DATA();
DeviceInfoData.cbSize = 28;
DeviceInfoData.devInst = 0;
DeviceInfoData.classGuid = System.Guid.Empty;
DeviceInfoData.reserved = 0;
UInt32 i;
StringBuilder DeviceName = new StringBuilder("");
DeviceName.Capacity = Externs.MAX_DEV_LEN;
for (i = 0; Externs.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++)
{
while (!Externs.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Externs.SPDRP_DEVICEDESC, 0, DeviceName, Externs.MAX_DEV_LEN, IntPtr.Zero))
{
}
bool bMatch = true;
foreach (string search in match)
{
if (!DeviceName.ToString().ToLower().Contains(search.ToLower()))
{
bMatch = false;
break;
}
}
if (bMatch)
{
OpenClose(hDevInfo, DeviceInfoData, bEnable);
}
}
Externs.SetupDiDestroyDeviceInfoList(hDevInfo);
}
catch (Exception ex)
{
throw new Exception("枚举设备信息出错!", ex);
return false;
}
return true;
}
/// <summary>
/// 允许一个窗口或者服务接收所有硬件的通知
/// 注:目前还没有找到一个比较好的方法来处理如果通知服务。
/// </summary>
/// <param name="callback"></param>
/// <param name="UseWindowHandle"></param>
/// <returns></returns>
public bool AllowNotifications(IntPtr callback, bool UseWindowHandle)
{
try
{
Externs.DEV_BROADCAST_DEVICEINTERFACE dbdi = new Externs.DEV_BROADCAST_DEVICEINTERFACE();
dbdi.dbcc_size = Marshal.SizeOf(dbdi);
dbdi.dbcc_reserved = 0;
dbdi.dbcc_devicetype = Externs.DBT_DEVTYP_DEVICEINTERFACE;
if (UseWindowHandle)
Externs.RegisterDeviceNotification(callback, dbdi, Externs.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES | Externs.DEVICE_NOTIFY_WINDOW_HANDLE);
else
Externs.RegisterDeviceNotification(callback, dbdi, Externs.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES | Externs.DEVICE_NOTIFY_SERVICE_HANDLE);
return true;
}
catch (Exception ex)
{
string err = ex.Message;
return false;
}
}
#endregion
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)