实用技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > ASP.NET > 实用技巧 > .NET与Windows硬件交互操作

.NET Core实现与Windows硬件交互操作的完整指南

作者:百锦再@新空间

这篇文章主要为大家详细介绍了.NET Core实现与Windows硬件交互操作的相关知识,包括Windows注册表操作,WMI查询等内容,感兴趣的小伙伴可以了解下

 .NET Core与Windows硬件交互技术全景

交互方法核心技术典型应用场景关键组件/类优势局限性
Windows注册表操作直接读写Windows注册表读取/修改系统配置、应用设置Registry, RegistryKey直接访问系统配置,功能全面需处理权限和异常,不当操作有风险
WMI查询Windows管理规范获取硬件信息(CPU、磁盘、网络等)ManagementObjectSearcher信息丰富,标准化接口仅查询,不能控制,性能开销较大
.NET IoT库通过USB转串口适配器GPIO、I2C、SPI通信Iot.Device.FtCommon, Ft232HDevice跨平台,硬件协议级访问需要特定硬件适配器支持
P/Invoke原生API调用Windows原生API触控板、低级输入设备DllImport, 原始输入API直接底层访问,功能强大复杂度高,需处理平台调用细节

1. Windows注册表操作

原理解说

Windows注册表是一个集中式的层次数据库,存储了操作系统和应用程序的配置信息。通过Microsoft.Win32命名空间,.NET Core应用可以访问和修改这些配置,间接实现与硬件的交互(如修改显示设置、电源管理等)。

核心组件

实战示例

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}");
        }
    }
}

关键注意事项

2. WMI硬件信息查询

原理解说

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}");
        }
    }
}

关键注意事项

3. .NET IoT库硬件协议控制

原理解说

.NET IoT库为GPIO、I2C、SPI等通用硬件协议提供了统一的API,可以通过USB转串口适配器(如FTDI FT232H)在普通PC上使用这些协议与外围设备通信。

核心组件

实战示例

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.KeyAvailable)
                {
                    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}");
        }
    }
}

关键注意事项

4. P/Invoke原生API调用

原理解说

对于没有托管封装的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硬件交互操作的完整指南的详细内容,更多关于.NET与Windows硬件交互操作的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文