C#實(shí)現(xiàn)系統(tǒng)信息監(jiān)控與獲取功能
前言
在 C# 開發(fā)的眾多應(yīng)用場(chǎng)景中,獲取系統(tǒng)信息以及監(jiān)控用戶操作有著廣泛的用途。比如在系統(tǒng)性能優(yōu)化工具中,需要實(shí)時(shí)讀取 CPU、GPU 資源信息;在一些特殊的輸入記錄程序里,可能會(huì)涉及到鍵盤監(jiān)控;而在圖形界面開發(fā)中,獲取屏幕大小是基礎(chǔ)操作。本文將詳細(xì)介紹如何使用 C# 來實(shí)現(xiàn)這些功能,助力大家在開發(fā)中更好地與系統(tǒng)底層進(jìn)行交互。
一、C# 監(jiān)控鍵盤
1. 原理與實(shí)現(xiàn)思路
在 Windows 系統(tǒng)下,可以通過 Windows API 來實(shí)現(xiàn)鍵盤監(jiān)控。需要使用SetWindowsHookEx函數(shù)來設(shè)置一個(gè)鉤子,當(dāng)鍵盤事件發(fā)生時(shí),系統(tǒng)會(huì)調(diào)用我們定義的回調(diào)函數(shù)來處理這些事件。
2. 代碼實(shí)現(xiàn)
首先,需要引入System.Runtime.InteropServices命名空間,以便調(diào)用 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; // 導(dǎo)入SetWindowsHookEx函數(shù) [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId); // 導(dǎo)入U(xiǎn)nhookWindowsHookEx函數(shù) [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(MarshalType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); // 導(dǎo)入CallNextHookEx函數(shù) [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); // 導(dǎo)入GetModuleHandle函數(shù) [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; // 定義回調(diào)函數(shù) 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("設(shè)置鉤子失敗。"); } } } // 卸載鉤子 public static void StopMonitoring() { if (hHook!= IntPtr.Zero) { UnhookWindowsHookEx(hHook); hHook = IntPtr.Zero; } } }
在Main方法中可以調(diào)用KeyboardMonitor.StartMonitoring()來開始監(jiān)控鍵盤,調(diào)用KeyboardMonitor.StopMonitoring()停止監(jiān)控。
二、讀取 CPU、GPU 資源信息
1. 使用 PerformanceCounter 讀取 CPU 信息
PerformanceCounter類是.NET 框架提供的用于讀取系統(tǒng)性能計(jì)數(shù)器的工具。通過它可以方便地獲取 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($"當(dāng)前CPU使用率: {cpuUsage}%"); System.Threading.Thread.Sleep(1000); }
2. 使用第三方庫讀取 GPU 信息
讀取 GPU 信息相對(duì)復(fù)雜一些,通常需要借助第三方庫,比如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負(fù)載: {sensor.Value}%"); } else if (sensor.SensorType == SensorType.Temperature) { Console.WriteLine($"GPU溫度: {sensor.Value}℃"); } } } } } ~GpuMonitor() { computer.Close(); } }
在Main方法中調(diào)用:
GpuMonitor gpuMonitor = new GpuMonitor(); gpuMonitor.PrintGpuInfo();
三、獲取屏幕大小
在 C# 中,可以使用System.Windows.Forms.Screen類來獲取屏幕相關(guān)信息,包括屏幕大小。
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方法中調(diào)用ScreenInfo.GetScreenSize()即可獲取屏幕大小信息。
四、總結(jié)
通過以上方法,我們利用 C# 實(shí)現(xiàn)了監(jiān)控鍵盤、讀取 CPU 和 GPU 資源信息以及獲取屏幕大小的功能。這些功能在系統(tǒng)性能分析、特殊輸入處理以及圖形界面適配等方面都有著重要的應(yīng)用。在實(shí)際開發(fā)中,大家可以根據(jù)具體需求對(duì)這些功能進(jìn)行拓展和優(yōu)化。
以上就是C#實(shí)現(xiàn)系統(tǒng)信息監(jiān)控與獲取功能的詳細(xì)內(nèi)容,更多關(guān)于C#系統(tǒng)信息監(jiān)控與獲取的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實(shí)現(xiàn)將程序鎖定到Win7任務(wù)欄的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將程序鎖定到Win7任務(wù)欄的方法,涉及C#調(diào)用Shell類的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08無法從 int? 轉(zhuǎn)換為 int 運(yùn)行時(shí)出現(xiàn)錯(cuò)誤
無法從"int?"轉(zhuǎn)換為"int" ,在運(yùn)行時(shí)會(huì)出現(xiàn)錯(cuò)誤,通過強(qiáng)制類型轉(zhuǎn)換(int)便可解決2014-05-05C# 中文簡(jiǎn)體轉(zhuǎn)繁體實(shí)現(xiàn)代碼
C# 中文簡(jiǎn)體轉(zhuǎn)繁體實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-02-02C#解決多IfElse判斷語句和Switch語句問題的方法分享
這篇文章主要為大家介紹C#如何使用設(shè)計(jì)模式中的策略模式和委托來解決多個(gè)IfElse判斷語句和Switch語句,這種替換方式在其他語言也一樣可以做到,感興趣的可以了解一下2022-12-12Unity ScrollRect實(shí)現(xiàn)軌跡滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity ScrollRect實(shí)現(xiàn)軌跡滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09C#操作Access數(shù)據(jù)庫的實(shí)現(xiàn)過程(vs2019)
這篇文章主要介紹了C#操作Access數(shù)據(jù)庫的實(shí)現(xiàn)過程(vs2019),打開Office Access新建一個(gè)空白數(shù)據(jù)庫DATA.accdb,并改好存放位置,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-02-02