C# 获取Windows系统:Cpu使用率,内存使用率,Mac地址,磁盘使用率
一、获取CPU使用率:
#region 获取CPU使用率 #region AIP声明
[DllImport("IpHlpApi.dll")]
extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder); [DllImport("User32")]
private extern static int GetWindow(int hWnd, int wCmd); [DllImport("User32")]
private extern static int GetWindowLongA(int hWnd, int wIndx); [DllImport("user32.dll")]
private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize); [DllImport("user32", CharSet = CharSet.Auto)]
private extern static int GetWindowTextLength(IntPtr hWnd);
#endregion
public static float? GetCpuUsedRate()
{
try
{
PerformanceCounter pcCpuLoad;
pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total")
{
MachineName = "."
};
pcCpuLoad.NextValue();
Thread.Sleep();
float CpuLoad= pcCpuLoad.NextValue();
return CpuLoad;
}
catch
{
}
return ;
}
#endregion
二、获取内存使用率
其中ManagementClass类需要手动引用System.Management,然后再using System.Management。
#region 获取内存使用率
#region 可用内存 /// <summary>
/// 获取可用内存
/// </summary>
internal static long? GetMemoryAvailable()
{
long availablebytes = ;
var managementClassOs = new ManagementClass("Win32_OperatingSystem");
foreach (var managementBaseObject in managementClassOs.GetInstances())
if (managementBaseObject["FreePhysicalMemory"] != null)
availablebytes = * long.Parse(managementBaseObject["FreePhysicalMemory"].ToString());
return availablebytes / MbDiv;
} #endregion
internal static double? GetMemoryUsed()
{
float? PhysicalMemory = GetPhysicalMemory();
float? MemoryAvailable = GetMemoryAvailable();
double? MemoryUsed = (double?)(PhysicalMemory - MemoryAvailable);
double currentMemoryUsed = (double)MemoryUsed ;
return currentMemoryUsed ;
}
private static long? GetPhysicalMemory()
{
//获得物理内存
var managementClass = new ManagementClass("Win32_ComputerSystem");
var managementObjectCollection = managementClass.GetInstances();
long PhysicalMemory;
foreach (var managementBaseObject in managementObjectCollection)
if (managementBaseObject["TotalPhysicalMemory"] != null)
{
return long.Parse(managementBaseObject["TotalPhysicalMemory"].ToString())/ MbDiv;
}
return null; }
public static double? GetMemoryUsedRate()
{
float? PhysicalMemory = GetPhysicalMemory();
float? MemoryAvailable = GetMemoryAvailable();
double? MemoryUsedRate =(double?)(PhysicalMemory - MemoryAvailable)/ PhysicalMemory;
return MemoryUsedRate.HasValue ? Convert.ToDouble(MemoryUsedRate * ) : ;
}
#endregion #region 单位转换进制 private const int KbDiv = ;
private const int MbDiv = * ;
private const int GbDiv = * * ; #endregion
三、获取Mac地址
#region 获取当前活动网络MAC地址
/// <summary>
/// 获取本机MAC地址
/// </summary>
/// <returns>本机MAC地址</returns>
public static string GetMacAddress()
{
try
{
string strMac = string.Empty;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
strMac = mo["MacAddress"].ToString();
}
}
moc = null;
mc = null;
return strMac;
}
catch
{
return "unknown";
}
}
#endregion
四、获取磁盘使用率
#region 获取磁盘占用率
internal static double GetUsedDiskPercent()
{
float? usedSize = GetUsedDiskSize();
float? totalSize = GetTotalSize();
double? percent = (double?)usedSize / totalSize;
return percent.HasValue ? Convert.ToDouble(percent * ) : ;
} internal static float? GetUsedDiskSize()
{
var currentDrive = GetCurrentDrive();
float UsedDiskSize =(long)currentDrive?.TotalSize - (long)currentDrive?.TotalFreeSpace;
return UsedDiskSize / MbDiv;
} internal static float? GetTotalSize()
{
var currentDrive = GetCurrentDrive(); float TotalSize = (long)currentDrive?.TotalSize / MbDiv;
return TotalSize;
} /// <summary>
/// 获取当前执行的盘符信息
/// </summary>
/// <returns></returns>
private static DriveInfo GetCurrentDrive()
{
string path = Application.StartupPath.ToString().Substring(, );
return DriveInfo.GetDrives().FirstOrDefault<DriveInfo>(p => p.Name.Equals(path));
}
#endregion
C# 获取Windows系统:Cpu使用率,内存使用率,Mac地址,磁盘使用率的更多相关文章
- C/C++获取Windows系统CPU和内存及硬盘使用情况
//1.获取Windows系统内存使用率 //windows 内存 使用率 DWORD getWin_MemUsage(){ MEMORYSTATUS ms; ::GlobalMemoryStatus ...
- C/C++获取Linux系统CPU和内存及硬盘使用情况
需求分析: 不使用Top df free 等命令,利用C/C++获取Linux系统CPU和内存及硬盘使用情况 实现: //通过获取/proc/stat (CPU)和/proc/meminfo(内存 ...
- Windows系统CPU和内存状态实时查询(Java)
一.背景 需要查询Windows服务器的CPU和内存状态. Linux系统查询CPU和内存状态很简单,一个top命令搞定,Windows就稍微麻烦一些了. 经过资料查找,发现jdk目前不能直接查询系统 ...
- PHP 之获取Windows下CPU、内存的使用率
<?php /** * Created by PhpStorm. * User: 25754 * Date: 2019/5/4 * Time: 13:42 */ class SystemInfo ...
- linux服务器性能——CPU、内存、流量、磁盘使用率的监控
https://blog.csdn.net/u012859748/article/details/72731080
- C#获取特定进程CPU和内存使用率
首先是获取特定进程对象,可以使用Process.GetProcesses()方法来获取系统中运行的所有进程,或者使用Process.GetCurrentProcess()方法来获取当前程序所对应的进程 ...
- Java获取Linux系统cpu使用率
原文:http://www.open-open.com/code/view/1426152165201 import java.io.BufferedReader; import java.io.Fi ...
- php获取linux服务器CPU、内存、硬盘使用率的实现代码
define("MONITORED_IP", "172.16.0.191"); //被监控的服务器IP地址 也就是本机地址 define("DB_SE ...
- Java如何获取系统cpu、内存、硬盘信息
1 概述 前段时间摸索在Java中怎么获取系统信息包括cpu.内存.硬盘信息等,刚开始使用Java自带的包进行获取,但这样获取的内存信息不够准确并且容易出现找不到相应包等错误,所以后面使用sigar插 ...
- Windows系统CPU内存网络性能统计第一篇 内存
最近翻出以前做过的Windows系统性能统计程序,这个程序可以统计系统中的CPU使用情况,内存使用情况以及网络流量.现在将其整理一下(共有三篇),希望对大家有所帮助. 目录如下: 1.<Wind ...
随机推荐
- Android OnGestureListener用法 识别用户手势 左右滑动
Android可以识别用户的手势(即用户用手指滑动的方向),通过用户不同的手势,从而做出不同的处理 需要使用OnGestureListener 比如说看电子书的时候翻页,或者要滑动一些其他内容 直接上 ...
- docker安装及问题处理
1.在Ubuntu的命令行中输入 sudo apt-get install docker.io 2.如果切换到了root用户下 apt-get install docker.io 3.对于新安装的Ub ...
- ssi的使用 开启 配置等
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/lsfhack/article/details/69664402ssi的定义SSI(Server Si ...
- Sass(SCSS)中文手册——入门
简书原文 https://www.jianshu.com/p/e82c27aa05c7 前言 该中文手册是我在Sass中文文档的基础上编辑的,或者也可以理解为就是Sass中文文档的翻版.之所以有这篇文 ...
- 可直接复制粘贴的boostrap图标库网址
1:http://fontawesome.dashgame.com/ 2:http://www.kuiyu.net/art-34.html 3:http://www.bootcss.com/p/fon ...
- leetcode笔记:Remove Duplicates from Sorted Array II
一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...
- 洛谷 P1709 隐藏口令Hidden Password
->题目链接 题解: 贪心+字符串 #include<iostream> #include<cstring> #define N 5000005 using namesp ...
- php实现 统计每个月兔子的数量
php实现 统计每个月兔子的数量 一.总结 一句话总结:想清楚再动,怎么可能出错嘛. 二.统计每个月兔子的数量 题目描述 有一只兔子,从出生后第3个月起每个月都生一只兔子,小兔子长到第三个月后每个月又 ...
- WeakRefence
http://183615215-qq-com.iteye.com/blog/1867568
- jquery-5 jQuery筛选选择器
jquery-5 jQuery筛选选择器 一.总结 一句话总结:选择器加动态添加方法可以不用想方法名,这个简单方便. 1.筛选选择器有哪三种? 过滤 查找 串联 1.过滤 eq();first(); ...