前言
在 c# 开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途。比如在系统性能优化工具中,需要实时读取 cpu、gpu 资源信息;在一些特殊的输入记录程序里,可能会涉及到键盘监控;而在图形界面开发中,获取屏幕大小是基础操作。本文将详细介绍如何使用 c# 来实现这些功能,助力大家在开发中更好地与系统底层进行交互。
一、c# 监控键盘
1. 原理与实现思路
在 windows 系统下,可以通过 windows api 来实现键盘监控。需要使用setwindowshookex函数来设置一个钩子,当键盘事件发生时,系统会调用我们定义的回调函数来处理这些事件。
2. 代码实现
首先,需要引入system.runtime.interopservices命名空间,以便调用 windows api。
using system; using system.runtime.interopservices; class keyboardmonitor { // 定义委托类型 private delegate intptr hookproc(int ncode, intptr wparam, intptr lparam); // 定义钩子句柄 private static intptr hhook = intptr.zero; // 导入setwindowshookex函数 [dllimport("user32.dll", charset = charset.auto, setlasterror = true)] private static extern intptr setwindowshookex(int idhook, hookproc lpfn, intptr hmod, uint dwthreadid); // 导入unhookwindowshookex函数 [dllimport("user32.dll", charset = charset.auto, setlasterror = true)] [return: marshalas(marshaltype.bool)] private static extern bool unhookwindowshookex(intptr hhk); // 导入callnexthookex函数 [dllimport("user32.dll", charset = charset.auto, setlasterror = true)] private static extern intptr callnexthookex(intptr hhk, int ncode, intptr wparam, intptr lparam); // 导入getmodulehandle函数 [dllimport("kernel32.dll", charset = charset.auto, setlasterror = true)] private static extern intptr getmodulehandle(string lpmodulename); // 定义钩子类型 private const int wh_keyboard_ll = 13; // 定义键盘消息常量 private const int wm_keydown = 0x0100; private const int wm_keyup = 0x0101; // 定义回调函数 private static intptr keyboardhookproc(int ncode, intptr wparam, intptr lparam) { if (ncode >= 0) { if (wparam == (intptr)wm_keydown || wparam == (intptr)wm_keyup) { int vkcode = marshal.readint32(lparam); console.writeline($"键盘事件: {(wparam == (intptr)wm_keydown? "按下" : "松开")},键码: {vkcode}"); } } return callnexthookex(hhook, ncode, wparam, lparam); } // 安装钩子 public static void startmonitoring() { using (system.diagnostics.process curprocess = system.diagnostics.process.getcurrentprocess()) using (system.diagnostics.processmodule curmodule = curprocess.mainmodule) { hhook = setwindowshookex(wh_keyboard_ll, keyboardhookproc, getmodulehandle(curmodule.modulename), 0); if (hhook == intptr.zero) { console.writeline("设置钩子失败。"); } } } // 卸载钩子 public static void stopmonitoring() { if (hhook!= intptr.zero) { unhookwindowshookex(hhook); hhook = intptr.zero; } } }
在main方法中可以调用keyboardmonitor.startmonitoring()来开始监控键盘,调用keyboardmonitor.stopmonitoring()停止监控。
二、读取 cpu、gpu 资源信息
1. 使用 performancecounter 读取 cpu 信息
performancecounter类是.net 框架提供的用于读取系统性能计数器的工具。通过它可以方便地获取 cpu 使用率等信息。
using system; using system.diagnostics; class cpumonitor { private performancecounter cpucounter; public cpumonitor() { cpucounter = new performancecounter("processor", "% processor time", "_total"); } public float getcpuusage() { return cpucounter.nextvalue(); } }
在main方法中使用如下:
cpumonitor cpumonitor = new cpumonitor(); while (true) { float cpuusage = cpumonitor.getcpuusage(); console.writeline($"当前cpu使用率: {cpuusage}%"); system.threading.thread.sleep(1000); }
2. 使用第三方库读取 gpu 信息
读取 gpu 信息相对复杂一些,通常需要借助第三方库,比如openhardwaremonitor。首先通过 nuget 安装openhardwaremonitor库。
using openhardwaremonitor.hardware; using system; class gpumonitor { private computer computer; public gpumonitor() { computer = new computer(); computer.gpuenabled = true; computer.open(); } public void printgpuinfo() { foreach (ihardware hardware in computer.hardware) { if (hardware.hardwaretype == hardwaretype.gpunvidia || hardware.hardwaretype == hardwaretype.gpuamd) { hardware.update(); foreach (isensor sensor in hardware.sensors) { if (sensor.sensortype == sensortype.load) { console.writeline($"gpu负载: {sensor.value}%"); } else if (sensor.sensortype == sensortype.temperature) { console.writeline($"gpu温度: {sensor.value}℃"); } } } } } ~gpumonitor() { computer.close(); } }
在main方法中调用:
gpumonitor gpumonitor = new gpumonitor(); gpumonitor.printgpuinfo();
三、获取屏幕大小
在 c# 中,可以使用system.windows.forms.screen类来获取屏幕相关信息,包括屏幕大小。
using system; using system.windows.forms; class screeninfo { public static void getscreensize() { screen primaryscreen = screen.primaryscreen; console.writeline($"屏幕宽度: {primaryscreen.bounds.width} 像素"); console.writeline($"屏幕高度: {primaryscreen.bounds.height} 像素"); } }
在main方法中调用screeninfo.getscreensize()即可获取屏幕大小信息。
四、总结
通过以上方法,我们利用 c# 实现了监控键盘、读取 cpu 和 gpu 资源信息以及获取屏幕大小的功能。这些功能在系统性能分析、特殊输入处理以及图形界面适配等方面都有着重要的应用。在实际开发中,大家可以根据具体需求对这些功能进行拓展和优化。
以上就是c#实现系统信息监控与获取功能的详细内容,更多关于c#系统信息监控与获取的资料请关注代码网其它相关文章!
发表评论