发布于2026-07-23 阅读(0)
扫一扫,手机访问
.NET Core与Windows硬件交互技术全景

| 交互方法 | 核心技术 | 典型应用场景 | 关键组件/类 | 优势 | 局限性 |
|---|---|---|---|---|---|
| Windows注册表操作 | 直接读写Windows注册表 | 读取/修改系统配置、应用设置 | Registry, RegistryKey | 直接访问系统配置,功能全面 | 需处理权限 and 异常,不当操作有风险 |
| WMI查询 | Windows管理规范 | 获取硬件信息(CPU、磁盘、网络等) | ManagementObjectSearcher | 信息丰富,标准化接口 | 仅查询,不能控制,性能开销较大 |
| .NET IoT库 | 通过USB转串口适配器 | GPIO、I2C、SPI通信 | Iot.Device.FtCommon, Ft232HDevice | 跨平台,硬件协议级访问 | 需要特定硬件适配器支持 |
| P/Invoke原生API | 调用Windows原生API | 触控板、低级输入设备 | DllImport, 原始输入API | 直接底层访问,功能强大 | 复杂度高,需处理平台调用细节 |
Windows注册表本质上是一个集中式的层次数据库,专门用来存放操作系统和应用程序的配置信息。通过.NET Core的Microsoft.Win32命名空间,开发者可以轻松访问和修改这些配置项,从而间接实现对硬件的控制——比如调整显示设置、管理电源方案等。说白了,它就是系统配置的“大本营”,通过它你能跟硬件“说上话”。
Registry.LocalMachine、Registry.CurrentUser这些常用的入口。using Microsoft.Win32;
using System;
public class RegistryOperations
{
public static void ReadWriteRegistry()
{
try
{
// 在HKEY_CURRENT_USER下创建或打开Software\MyApp子项
using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\MyApp"))
{
if (key != null)
{
// 写入不同类型的值
key.SetValue("Version", "1.0.0", RegistryValueKind.String);
key.SetValue("LastRun", DateTime.Now.ToString(), RegistryValueKind.String);
key.SetValue("StartWithOS", 1, RegistryValueKind.DWord);
// 读取值
string version = key.GetValue("Version") as string;
string lastRun = key.GetValue("LastRun") as string;
int startWithOS = (int)key.GetValue("StartWithOS", 0);
Console.WriteLine($"版本: {version}, 最后运行: {lastRun}, 随系统启动: {startWithOS}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"注册表操作失败: {ex.Message}");
}
}
// 查询系统信息示例
public static void ReadSystemInfo()
{
try
{
// 读取Windows版本信息
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
{
if (key != null)
{
string productName = key.GetValue("ProductName") as string;
string currentVersion = key.GetValue("CurrentVersion") as string;
Console.WriteLine($"操作系统: {productName}");
Console.WriteLine($"系统版本: {currentVersion}");
}
}
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("权限不足,无法访问系统注册表");
}
catch (Exception ex)
{
Console.WriteLine($"查询系统信息失败: {ex.Message}");
}
}
}
HKEY_LOCAL_MACHINE通常需要管理员权限,否则会碰一鼻子灰。RegistryKey实现了IDisposable接口,务必用using语句包裹,确保资源及时释放,别让系统“内存泄漏”找上门。RegistryValueKind明确指定,能避免数据存储出岔子。SecurityException、UnauthorizedAccessException等异常,做好异常处理是基本功。WMI(Windows Management Instrumentation)是Windows系统管理的基础设施,它提供了一套统一的接口,让你能轻松获取系统硬件和软件的各种信息。WMI使用类似SQL的查询语言(WQL)来查询系统信息,写起来很直观。
using System;
using System.Management;
using System.Collections.Generic;
public class HardwareInfoQuery
{
public static void QueryHardwareInfo()
{
// 示例1:查询CPU信息
QueryCPUInfo();
// 示例2:查询磁盘信息
QueryDiskInfo();
// 示例3:查询网络适配器
QueryNetworkAdapters();
}
public static void QueryCPUInfo()
{
try
{
string query = "SELECT * FROM Win32_Processor";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
ManagementObjectCollection results = searcher.Get();
Console.WriteLine("=== CPU信息 ===");
foreach (ManagementObject mo in results)
{
string name = mo["Name"] as string;
string manufacturer = mo["Manufacturer"] as string;
string maxClockSpeed = mo["MaxClockSpeed"] as string;
string cores = mo["NumberOfCores"] as string;
Console.WriteLine($"处理器: {name}");
Console.WriteLine($"制造商: {manufacturer}");
Console.WriteLine($"最大主频: {maxClockSpeed} MHz");
Console.WriteLine($"核心数: {cores}");
Console.WriteLine("------------------------");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"查询CPU信息失败: {ex.Message}");
}
}
public static void QueryDiskInfo()
{
try
{
string query = "SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
ManagementObjectCollection results = searcher.Get();
Console.WriteLine("=== 磁盘信息 ===");
foreach (ManagementObject mo in results)
{
string deviceId = mo["DeviceID"] as string;
string size = mo["Size"] as string;
string freeSpace = mo["FreeSpace"] as string;
if (long.TryParse(size, out long totalSize) &&
long.TryParse(freeSpace, out long free))
{
double totalGB = Math.Round(totalSize / (1024.0 * 1024.0 * 1024.0), 2);
double freeGB = Math.Round(free / (1024.0 * 1024.0 * 1024.0), 2);
double usedPercent = Math.Round((totalSize - free) * 100.0 / totalSize, 2);
Console.WriteLine($"磁盘: {deviceId}");
Console.WriteLine($"总大小: {totalGB} GB");
Console.WriteLine($"可用空间: {freeGB} GB");
Console.WriteLine($"使用率: {usedPercent}%");
Console.WriteLine("------------------------");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"查询磁盘信息失败: {ex.Message}");
}
}
public static void QueryNetworkAdapters()
{
try
{
string query = "SELECT * FROM Win32_NetworkAdapter WHERE NetEnabled = True";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
ManagementObjectCollection results = searcher.Get();
Console.WriteLine("=== 网络适配器 ===");
foreach (ManagementObject mo in results)
{
string name = mo["Name"] as string;
string adapterType = mo["AdapterType"] as string;
string macAddress = mo["MACAddress"] as string;
string netEnabled = mo["NetEnabled"]?.ToString();
if (!string.IsNullOrEmpty(macAddress))
{
Console.WriteLine($"适配器名称: {name}");
Console.WriteLine($"类型: {adapterType}");
Console.WriteLine($"MAC地址: {macAddress}");
Console.WriteLine("------------------------");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"查询网络适配器失败: {ex.Message}");
}
}
}
System.Management包引用,否则编译通不过。.NET IoT库为GPIO、I2C、SPI等通用硬件协议提供了统一的API,通过USB转串口适配器(比如FTDI FT232H),你就可以在普通PC上使用这些协议与外围设备通信了。这相当于给普通电脑装上了“硬件接口”,能直接跟传感器、LED等设备打交道。
using System;
using System.Device.Gpio;
using System.Device.I2c;
using System.Threading;
using Iot.Device.Ft232H;
using Iot.Device.FtCommon;
using Iot.Device.Bmxx80;
using Iot.Device.Bmxx80.PowerMode;
public class IotHardwareControl
{
public static void ControlGpioAndI2c()
{
// 检查可用的FTDI设备
var devices = FtCommon.GetDevices();
if (devices.Count == 0)
{
Console.WriteLine("未找到FTDI设备,请确保设备已连接且驱动程序已安装");
return;
}
Console.WriteLine($"找到 {devices.Count} 个FTDI设备");
// 使用第一个设备
var device = devices[0];
Console.WriteLine($"使用设备: {device.Description}");
// GPIO控制示例
GpioControlExample(device);
// I2C设备示例
I2cDeviceExample(device);
}
public static void GpioControlExample(FtDevice device)
{
try
{
using (var ft232h = new Ft232HDevice(device))
using (var gpio = ft232h.CreateGpioController())
{
// 获取引脚号(D7对应FT232H上的物理引脚)
int ledPin = Ft232HDevice.GetPinNumberFromString("D7");
// 打开引脚并设置为输出模式
gpio.OpenPin(ledPin, PinMode.Output);
Console.WriteLine("GPIO控制示例: LED闪烁(按任意键停止)");
// LED闪烁
bool ledOn = true;
while (!Console.KeyA vailable)
{
gpio.Write(ledPin, ledOn ? PinValue.High : PinValue.Low);
Thread.Sleep(500);
ledOn = !ledOn;
}
Console.ReadKey(); // 清除按键
// 关闭LED并清理资源
gpio.Write(ledPin, PinValue.Low);
gpio.ClosePin(ledPin);
}
}
catch (Exception ex)
{
Console.WriteLine($"GPIO控制失败: {ex.Message}");
}
}
public static void I2cDeviceExample(FtDevice device)
{
try
{
using (var ft232h = new Ft232HDevice(device))
{
// 创建I2C连接设置
var i2cSettings = new I2cConnectionSettings(0, 0x76);
// 创建I2C设备
using (var i2cDevice = ft232h.CreateI2cDevice(i2cSettings))
using (var bme280 = new Bme280(i2cDevice))
{
// 配置传感器
bme280.SetPowerMode(Bmx280PowerMode.Forced);
int measurementTime = bme280.GetMeasurementDuration();
Console.WriteLine("I2C传感器读取示例(BME280):");
// 读取传感器数据
for (int i = 0; i < 5; i++)
{
bme280.SetPowerMode(Bmx280PowerMode.Forced);
Thread.Sleep(measurementTime);
if (bme280.TryReadTemperature(out var temperature) &&
bme280.TryReadPressure(out var pressure) &&
bme280.TryReadHumidity(out var humidity))
{
Console.WriteLine($"温度: {temperature.DegreesCelsius:0.##}°C");
Console.WriteLine($"压力: {pressure.Hectopascals:0.##} hPa");
Console.WriteLine($"湿度: {humidity.Percent:0.##}%");
Console.WriteLine("------------------------");
}
Thread.Sleep(2000);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"I2C设备操作失败: {ex.Message}");
}
}
}
对于那些没有托管封装的Windows API,P/Invoke(Platform Invocation Services)是直接调用原生DLL函数的利器。这种方法适用于需要直接与低级硬件或系统服务交互的场景,比如读取触控板数据或控制低级输入设备。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class RawInputTouchpad
{
// 定义Windows API
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern bool RegisterRawInputDevices(
RAWINPUTDEVICE[] pRawInputDevices,
uint uiNumDevices,
uint cbSize);
[DllImport("user32.dll")]
private static extern uint GetRawInputData(
IntPtr hRawInput,
uint uiCommand,
IntPtr pData,
ref uint pcbSize,
uint cbSizeHeader);
// 常量定义
private const int WM_INPUT = 0x00FF;
private const int RIDEV_INPUTSINK = 0x00000100;
private const ushort HID_USAGE_PAGE_GENERIC = 0x01;
private const ushort HID_USAGE_GENERIC_MOUSE = 0x02;
// 结构体定义
[StructLayout(LayoutKind.Sequential)]
public struct RAWINPUTDEVICE
{
public ushort usUsagePage;
public ushort usUsage;
public uint dwFlags;
public IntPtr hwndTarget;
}
private IntPtr _windowHandle;
public RawInputTouchpad(IntPtr windowHandle)
{
_windowHandle = windowHandle;
RegisterForRawInput();
}
private void RegisterForRawInput()
{
RAWINPUTDEVICE[] devices = new RAWINPUTDEVICE[1];
// 注册鼠标输入(包括触控板)
devices[0].usUsagePage = HID_USAGE_PAGE_GENERIC;
devices[0].usUsage = HID_USAGE_GENERIC_MOUSE;
devices[0].dwFlags = RIDEV_INPUTSINK;
devices[0].hwndTarget = _windowHandle;
if (!RegisterRawInputDevices(devices, (uint)devices.Length,
(uint)Marshal.SizeOf(typeof(RAWINPUTDEVICE))))
{
throw new ApplicationException("注册原始输入设备失败");
}
}
// 在窗体中处理WM_INPUT消息
public void ProcessInputMessage(Message m)
{
if (m.Msg == WM_INPUT)
{
uint dataSize = 0;
// 获取原始输入数据大小
GetRawInputData(m.LParam, 0x10000003, IntPtr.Zero,
ref dataSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER)));
if (dataSize > 0)
{
IntPtr buffer = Marshal.AllocHGlobal((int)dataSize);
try
{
// 获取实际数据
if (GetRawInputData(m.LParam, 0x10000003, buffer,
ref dataSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) == dataSize)
{
// 解析触控板数据
ProcessTouchpadData(buffer);
}
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
}
}
private void ProcessTouchpadData(IntPtr rawData)
{
// 这里解析原始输入数据
// 实际实现需要根据触控板设备的数据格式进行解析
Console.WriteLine("收到触控板输入数据");
}
}
// 在Windows窗体中使用
public partial class MainForm : Form
{
private RawInputTouchpad _touchpad;
public MainForm()
{
InitializeComponent();
_touchpad = new RawInputTouchpad(this.Handle);
}
protected override void WndProc(ref Message m)
{
_touchpad?.ProcessInputMessage(m);
base.WndProc(ref m);
}
}
以上就是.NET Core实现与Windows硬件交互操作的完整指南的详细内容,希望对大家有所帮助。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8