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 ...
随机推荐
- centos php 安装memcached 扩展 支持sasl
1.安装sasl yum install cyrus-sasl-lib.x86_64 yum install cyrus-sasl-devel.x86_64 2.下载libmemcached wget ...
- [Angular] Stagger animation v4.3.3
For example, when we open a form, we want to see all the inputs fields comes into one by one. Code f ...
- Hadoop读书笔记(一)Hadoop介绍
1.Hadoop是什么? 适合大数据的分布式与计算平台. HDFS:Hadoop Distributed File System 分布式文件系统 MapReduce:并行计算框架 2.HDFS的架构 ...
- C_C++指针指针应用详解
前言:复杂类型说明 要了解指针,多多少少会出现一些比较复杂的类型,所以我先介绍一下如何完全理解一个复杂类型,要理解复杂类型其实很简单,一个类型里会出现很多运算符,他们也像普通的表达式一样,有优先级,其 ...
- [Ramda] Getter and Setter in Ramda & lens
Getter on Object: 1. prop: R.prop(}); //=> 100 R.prop('x', {}); //=> undefined 2. props: R.pro ...
- C API函数描写叙述(S-W)
25.2.3.59. mysql_select_db() int mysql_select_db(MYSQL *mysql, const char *db) 描写叙述 使由db指定的数据库成为由mys ...
- 格式化上下文formatting contexts
原文 简书原文:https://www.jianshu.com/p/20c32c367b63 什么是格式化上下文(formatting contexts) Formatting context是W3C ...
- HDoj-1163- Digital Roots
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- 利用a标签导出csv文件
原文 简书原文:https://www.jianshu.com/p/a8687610cda3 大纲 1.需求分析 2.通过a标签实现文件导出 3.实现方式 1.需求分析 导出文件,使用最多的方式还是服 ...
- HDU1575-Tr A(矩阵高速幂)
题目链接 题意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. 思路:简单的矩阵高速幂 代码: #include <iostream> #i ...