ManagementObjectSearcher使用

 PhysicalDriveDialog d = new PhysicalDriveDialog();//获取物理磁盘
  SafeFileHandle driveHandle = CreateFile(@d.physicalDrive, FileAccess.Read, FileShare.ReadWrite, 0, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
  if (driveHandle.IsInvalid)
                        throw new Win32Exception(Marshal.GetLastWin32Error());//如果获取不了
   DDStream = new FileStream(driveHandle, FileAccess.Read);             

ManagementObjectSearcher使用

// 硬件
Win32_Processor, // CPU 处理器
Win32_PhysicalMemory, // 物理内存条
Win32_Keyboard, // 键盘
Win32_PointingDevice, // 点输入设备,包括鼠标。
Win32_FloppyDrive, // 软盘驱动器
Win32_DiskDrive, // 硬盘驱动器
Win32_CDROMDrive, // 光盘驱动器
Win32_BaseBoard, // 主板
Win32_BIOS, // BIOS 芯片
Win32_ParallelPort, // 并口
Win32_SerialPort, // 串口
Win32_SerialPortConfiguration, // 串口配置
Win32_SoundDevice, // 多媒体设置,一般指声卡。
Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)
Win32_USBController, // USB 控制器
Win32_NetworkAdapter, // 网络适配器
Win32_NetworkAdapterConfiguration, // 网络适配器设置
Win32_Printer, // 打印机
Win32_PrinterConfiguration, // 打印机设置
Win32_PrintJob, // 打印机任务
Win32_TCPIPPrinterPort, // 打印机端口
Win32_POTSModem, // MODEM
Win32_POTSModemToSerialPort, // MODEM 端口
Win32_DesktopMonitor, // 显示器
Win32_DisplayConfiguration, // 显卡
Win32_DisplayControllerConfiguration, // 显卡设置
Win32_VideoController, // 显卡细节。
Win32_VideoSettings, // 显卡支持的显示模式。

// 操作系统
Win32_TimeZone, // 时区
Win32_SystemDriver, // 驱动程序
Win32_DiskPartition, // 磁盘分区
Win32_LogicalDisk, // 逻辑磁盘
Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。
Win32_LogicalMemoryConfiguration, // 逻辑内存配置
Win32_PageFile, // 系统页文件信息
Win32_PageFileSetting, // 页文件设置
Win32_BootConfiguration, // 系统启动配置
Win32_ComputerSystem, // 计算机信息简要
Win32_OperatingSystem, // 操作系统信息
Win32_StartupCommand, // 系统自动启动程序
Win32_Service, // 系统安装的服务
Win32_Group, // 系统管理组
Win32_GroupUser, // 系统组帐号
Win32_UserAccount, // 用户帐号
Win32_Process, // 系统进程
Win32_Thread, // 系统线程
Win32_Share, // 共享
Win32_NetworkClient, // 已安装的网络客户端
Win32_NetworkProtocol, // 已安装的网络协议
Win32_PnPEntity,//all device

作者:_那个谁
来源:CSDN
原文:https://blog.csdn.net/xingkong886/article/details/78285605
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据网友提供代码,逆向画出来的。包含CPU使用率、内存使用率、虚拟内存使用率、内存总量等。数据比较符合靠谱。 ***************************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Threading; using System.Runtime.InteropServices; using System.Management; namespace Cpu_Test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Process[] MyProcesses; Thread td; private void myUser() { ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor"); foreach (ManagementObject myobject in searcher.Get()) { tssluse.Text = myobject["LoadPercentage"].ToString() + " %"; lblCPU.Text = myobject["LoadPercentage"].ToString() + " %"; mheight = Convert.ToInt32(myobject["LoadPercentage"].ToString()); if (mheight == 100) panel3.Height = 100; CreateImage(); Memory(); } } private void Memory() { Microsoft.VisualBasic.Devices.Computer myInfo = new Microsoft.VisualBasic.Devices.Computer(); //获取物理内存总量 pbMemorySum.Maximum = Convert.ToInt32(myInfo.Info.TotalPhysicalMemory / 1024 / 1024); pbMemorySum.Value = Convert.ToInt32(myInfo.Info.TotalPhysicalMemory / 1024 / 1024); lblSum.Text = (myInfo.Info.TotalPhysicalMemory / 1024).ToString(); //获取可用物理内存总量 pbMemoryUse.Maximum = Convert.ToInt32(myInfo.Info.TotalPhysicalMemory / 1024 / 1024); pbMemoryUse.Value = Convert.ToInt32(myInfo.Info.AvailablePhysicalMemory / 1024 / 1024); lblMuse.Text = (myInfo.Info.AvailablePhysicalMemory / 1024).ToString(); //获取虚拟内存总量 pbVmemorysum.Maximum = Convert.ToInt32(myInfo.Info.TotalVirtualMemory / 1024 / 1024); pbVmemorysum.Value = Convert.ToInt32(myInfo.Info.TotalVirtualMemory / 1024 / 1024); lblVinfo.Text = (myInfo.Info.TotalVirtualMemory / 1024).ToString(); //获取可用虚拟内存总量 pbVmemoryuse.Maximum = Convert.ToInt32(myInfo.Info.TotalVirtualMemory / 1024 / 1024); pbVmemoryuse.Value = Convert.ToInt32(myInfo.Info.AvailableVirtualMemory / 1024 / 1024); lblVuse.Text = (myInfo.Info.AvailableVirtualMemory / 1024).ToString(); } private void Form1_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; MyProcesses = Process.GetProcesses(); //tsslNum.Text = "进程数: "; tsslNum.Text = "进程数: "+ MyProcesses.Length.ToString()+ " CPU使用: "; myUser(); } private void timer1_Tick(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; MyProcesses = Process.GetProcesses(); tsslNum.Text = "进程数: " + MyProcesses.Length.ToString() + " | CPU使用:"; td = new Thread(new ThreadStart(myUser)); td.Start(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { if (td != null) { td.Abort(); } } int mheight = 0; private void CreateImage() { int i = panel3.Height / 100; Bitmap image = new Bitmap(panel3.Width, panel3.Height); Graphics g = Graphics.FromImage(image); g.Clear(Color.Green); SolidBrush mybrush = new SolidBrush(Color.Lime); g.FillRectangle(mybrush, 0, panel3.Height - mheight * i, 26, mheight * i); panel3.BackgroundImage = image; } } }
通过 `ManagementObjectSearcher` 类,可以使用 WMI (Windows Management Instrumentation) 查询系统信息。以下是使用 `ManagementObjectSearcher` 获取磁盘信息的示例代码: ```csharp using System.Management; // 获取磁盘信息 string driveName = System.IO.Path.GetPathRoot(System.Reflection.Assembly.GetExecutingAssembly().Location); ManagementObjectSearcher searcher = new ManagementObjectSearcher(string.Format("SELECT * FROM Win32_LogicalDisk WHERE DeviceID='{0}'", driveName)); foreach (ManagementObject queryObj in searcher.Get()) { // 获取磁盘总量 long totalSizeInBytes = (long)queryObj["Size"]; // 获取磁盘使用量 long usedSizeInBytes = (long)queryObj["Size"] - (long)queryObj["FreeSpace"]; // 获取磁盘剩余空间信息 long freeSpaceInBytes = (long)queryObj["FreeSpace"]; // 获取当前应用程序使用量 long currentAppSizeInBytes = 0; foreach (var process in System.Diagnostics.Process.GetProcesses()) { try { currentAppSizeInBytes += process.WorkingSet64; } catch (System.ComponentModel.Win32Exception) { // 忽略访问权限不足的进程 } } // 将结果输出到控制台 Console.WriteLine("Total size: {0} bytes", totalSizeInBytes); Console.WriteLine("Used size: {0} bytes", usedSizeInBytes); Console.WriteLine("Free space: {0} bytes", freeSpaceInBytes); Console.WriteLine("Current app size: {0} bytes", currentAppSizeInBytes); } ``` 需要注意的是,这种方式需要使用管理员权限运行程序。另外,由于查询 WMI 信息可能会影响系统性能,建议尽量避免频繁查询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值