监控电脑CPU,内存,文件大小,硬盘空间,IP,用户名
- public class MonitorTools
- {
- /// <summary>
- /// 获取具体进程的内存,线程等参数情况
- /// </summary>
- /// <param name="processName"></param>
- public static void getWorkingSet(string processName)
- {
- Process[] ps = Process.GetProcesses();
- foreach (Process p in ps)
- {
- if (p.MainWindowHandle != null)
- {
- if (processName == p.ProcessName)
- {
- using (var process = Process.GetProcessesByName(processName)[])
- using (var p1 = new PerformanceCounter("Process", "Working Set - Private", processName))
- using (var p2 = new PerformanceCounter("Process", "Working Set", processName))
- {
- Console.WriteLine("{0}{1:N} KB", "工作集(进程类)", process.WorkingSet64 / );
- Console.WriteLine("{0}{1:N} KB", "内存(专用工作集)", p1.NextValue() / );
- Console.WriteLine(process.Threads.Count.ToString());//线程
- Console.WriteLine(process.Id.ToString());//PID
- }
- }
- }
- }
- }
- /// <summary>
- /// 遍历获取所有硬盘的可用空间
- /// </summary>
- /// <returns></returns>
- public static string GetDriveInfo()
- {
- string result = string.Empty;
- foreach (DriveInfo item in DriveInfo.GetDrives())
- {
- if (item.DriveType == DriveType.Fixed)
- {
- result += string.Format("{0} 可用空间{1:N} \r\n", item.Name, item.AvailableFreeSpace / ( * ) + "MB");
- }
- }
- return result;
- }
- /// <summary>
- /// 根据文件路径获得文件的大小信息
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public static string GetFileSize(string filePath)
- {
- string length = string.Empty;
- try
- {
- FileInfo file = new FileInfo(filePath);
- if (file != null)
- {
- length = file.Length.ToString();
- }
- return length;
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex.Message);
- return string.Empty;
- }
- }
- //测试获取CPU,内存等运行情况
- [DllImport("kernel32")]
- public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
- [DllImport("kernel32")]
- public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
- //定义CPU的信息结构
- [StructLayout(LayoutKind.Sequential)]
- public struct CPU_INFO
- {
- public uint dwOemId;
- public uint dwPageSize;
- public uint lpMinimumApplicationAddress;
- public uint lpMaximumApplicationAddress;
- public uint dwActiveProcessorMask;
- public uint dwNumberOfProcessors;
- public uint dwProcessorType;
- public uint dwAllocationGranularity;
- public uint dwProcessorLevel;
- public uint dwProcessorRevision;
- }
- //定义内存的信息结构
- [StructLayout(LayoutKind.Sequential)]
- public struct MEMORY_INFO
- {
- public uint dwLength;
- public uint dwMemoryLoad;
- public uint dwTotalPhys;
- public uint dwAvailPhys;
- public uint dwTotalPageFile;
- public uint dwAvailPageFile;
- public uint dwTotalVirtual;
- public uint dwAvailVirtual;
- }
- /// <summary>
- /// 调用GetSystemInfo函数获取CPU的相关信息
- /// </summary>
- /// <returns></returns>
- public static string GetCPUInfo()
- {
- try
- {
- string CPUresult = string.Empty;
- CPU_INFO CpuInfo;
- CpuInfo = new CPU_INFO();
- GetSystemInfo(ref CpuInfo);
- CPUresult += "本计算机中有" + CpuInfo.dwNumberOfProcessors.ToString() + "个CPU";
- CPUresult += "CPU的类型为" + CpuInfo.dwProcessorType.ToString();
- CPUresult += "CPU等级为" + String.Format("{0:N}", CpuInfo.dwProcessorLevel.ToString());
- CPUresult += "CPU的OEM ID为" + String.Format("{0:N}", CpuInfo.dwOemId.ToString());
- CPUresult += "CPU中的页面大小为" + String.Format("{0:N}", CpuInfo.dwPageSize.ToString());
- return CPUresult;
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex.Message);
- return string.Empty;
- }
- }
- /// <summary>
- /// 调用GlobalMemoryStatus函数获取内存的相关信息
- /// </summary>
- /// <returns></returns>
- public static string GetMemoryInfo()
- {
- try
- {
- string Memoryresult = string.Empty;
- MEMORY_INFO MemInfo;
- MemInfo = new MEMORY_INFO();
- GlobalMemoryStatus(ref MemInfo);
- Memoryresult += MemInfo.dwMemoryLoad.ToString() + "%的内存正在使用";
- Memoryresult += "物理内存共有" + (MemInfo.dwTotalPhys) / ( * ) + " MB";
- Memoryresult += "可使用的物理内存有" + (MemInfo.dwAvailPhys) / ( * ) + " MB";
- Memoryresult += "交换文件总大小为" + (MemInfo.dwTotalPageFile) / ( * ) + " MB";
- Memoryresult += "尚可交换文件大小为" + (MemInfo.dwAvailPageFile) / ( * ) + " MB";
- Memoryresult += "总虚拟内存有" + (MemInfo.dwTotalVirtual) / ( * ) + " MB";
- Memoryresult += "未用虚拟内存有" + (MemInfo.dwAvailVirtual) / ( * ) + " MB";
- return Memoryresult;
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex.Message);
- return string.Empty;
- }
- }
- }
- /// <summary>
- /// 根据PID结束指定进程
- /// </summary>
- /// <param name="pid"></param>
- public static bool EndProcess(int pid)
- {
- try
- {
- Process process = Process.GetProcessById(pid);
- process.Kill();
- return true;
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex.Message);
- return false;
- }
- }
- /// <summary>
- /// 监控线程的运行情况,如果退出,则记录退出的错误码和退出时间
- /// </summary>
- /// <param name="pid"></param>
- /// <returns></returns>
- public static string MonitorProcess(int pid)
- {
- string Message = string.Empty;
- try
- {
- Process process = Process.GetProcessById(pid);
- if (process.HasExited)
- {
- Message = string.Format("ExitCode:{0};ExitTime:{1}", process.ExitCode, process.ExitTime.ToString("yyyy年MM月dd HH时mm分ss秒"));
- }
- return Message;
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex.Message);
- return string.Empty;
- }
- }
- /// <summary>
- /// 调用GetSystemInfo函数获取CPU的相关信息,获取CPU占用率
- /// </summary>
- /// <returns></returns>
- public static string GetCPUInfo()
- {
- string CPUresult = string.Empty;
- try
- {
- PerformanceCounter pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
- pcCpuLoad.MachineName = ".";
- pcCpuLoad.NextValue();
- float cpuLoad = pcCpuLoad.NextValue();
- string.Format("CPU占用率:{0} %",cpuLoad);
- return CPUresult;
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex.Message);
- return string.Empty;
- }
- }
C#获取客服端ip和用户名:
- . 在asp.Net中专用属性:
- 获取服务器电脑名:page.server.manchinename
- 获取用户信息:page.user
- 获取客户端电脑名:page.request.userhostname
- 获取客户端电脑ip:page.request.userhostaddress
- . 在网络编程中的通用方法:
- 获取当前电脑名:static system.Net.dns.gethostname()
- 根据电脑名取出全部ip地址:static system.Net.dns.resolve(电脑名).addresslist
- 也可根据ip地址取出电脑名:static system.Net.dns.resolve(ip地址).hostname
- . 系统环境类的通用属性:
- 当前电脑名:static system.environment.machinename
- 当前电脑所属网域:static system.environment.userdomainname
- 当前电脑用户:static system.environment.username
- 举例子来说明:
- using system.Net;
- private void buttonip_click(object sender, system.eventargs e)
- {
- system.Net.ipaddress[] addresslist = dns.gethostbyname(dns.gethostname()).addresslist;
- if (addresslist.length>)
- {
- textlip.text = addresslist[].tostring();
- textsip.text = addresslist[].tostring();
- }
- else
- {
- textlip.text = addresslist[].tostring();
- textsip.text = "没有可用的连接";
- }
- }
监控电脑CPU,内存,文件大小,硬盘空间,IP,用户名的更多相关文章
- Linux 性能监控之CPU&内存&I/O监控Shell脚本2
Linux 性能监控之CPU&内存&I/O监控Shell脚本2 by:授客 QQ:1033553122 思路: 捕获数据->停止捕获数据->提取数据 备注:一些命令的输 ...
- Linux 性能监控之CPU&内存&I/O监控Shell脚本1
Linux 性能监控之CPU&内存&I/O监控Shell脚本1 by:授客 QQ:1033553122 #!/bin/bash # 获取要监控的本地服务器IP地址 IP=`if ...
- grafana+influxdb+telegraf监控服务器cpu,内存和硬盘
随便抄了一篇,目前我们的项目也在用,这个是linux和windows通吃的一种监控方案,非常有效,详细和优美,需要监控什么具体的业务内容,自己向influxdb中插入就行了. 监控服务器状态是运维必不 ...
- 使用python函数持续监控电脑cpu使用率、内存、c盘使用率等
方法一: # import time 导入time模块 # import psutil 导入psutil模块 # def func(): # while True: ------->持续监控得w ...
- Mac - 苹果电脑mac系统释放硬盘空间方法汇总
硬盘空间是大家最头痛的一个问题,大家在硬盘空间变小的时候怎么腾空间的呢?下面为大家分享7个mac系统释放空间的高级方法,大家赶紧来收了! mac系统释放硬盘空间方法: 方法一:删除Emacs--可以节 ...
- linux查看系统CPU,内存,硬盘使用情况
top查看CPU,内存使用情况 free查看硬盘使用情况
- Linux下查看CPU型号,内存大小,硬盘空间命令
1 查看CPU 1.1 查看CPU个数 # cat /proc/cpuinfo | grep "physical id" | uniq | wc -l 2 **uniq命令:删除重 ...
- Linux下查看CPU型号,内存大小,硬盘空间,进程等的命令(详解)
转自:http://www.jb51.net/article/97157.htm 1 查看CPU 1.1 查看CPU个数 # cat /proc/cpuinfo | grep "physic ...
- Linux下查看CPU型号,内存大小,硬盘空间的命令
1 查看CPU 1.1 查看CPU个数 # cat /proc/cpuinfo | grep "physical id" | uniq | wc -l 2 **uniq命令:删除重 ...
随机推荐
- php curl采集,服务器gzip压缩返回数据怎么办
一般服务器不会胡乱返回gzip压缩的数据,一般是客户端请求的头部里包含你浏览器能接受的压缩方式, Accept-Encoding:gzip,deflate,sdch 这里是gzip .deflat ...
- 自动化测试工具 Test Studio入门教程
Test Studio安装 可以到下载试用版 官网 http://www.telerik.com/teststudio , 装完以后需要装silverlight 安装好了,主界面是介个样子的 Test ...
- C 语言实例 - 输出九九乘法口诀表
C 语言实例 - 输出九九乘法口诀表 使用嵌套 for 循环输出九九乘法口诀表. 实例 #include<stdio.h> int main(){ //外层循环变量,控制行 ; //内层循 ...
- linux 初始配置(centos)-网络和可视化界面
1. 执行命令查看ip:ip addr 2. 如果没有或取到,要查看网卡是否启动,及ip配置是否正确,请先将ONBOOT改成yes,表示开机即启动 [lobin@localhost ~]$ cat / ...
- STP-11-多生成树:IEEE 802.1s
IEEE802.1s多生成树(MultipleSpanningTrees,MST)有时也称为多STP(MultipleSTP,MSTP),它定义了在使用802.1QVLAN网络中,部署多实例STP的标 ...
- Mac OS X
Mac OS X 除了微软自家的Windows平台, .NET Core针对Mac OS以及各种Linux(RHEL.Ubuntu.Debian.Fedora.CentOS和SUSE等)都提供了很好的 ...
- 我的grunt配置
module.exports = function(grunt) { // 配置. grunt.initConfig({ pkg: grunt.file.readJSON('package.json' ...
- Java 8特性尝鲜:新新IO
Java 8特性尝鲜:新新IO 在这个专题前面的文章中,我们已经看到,使用Java8的lambda表达式对现有的JDK1.2 I/O库的提升,主要是可以使用lambda表达式来构造java.io.Fi ...
- UIcollectionView 实现 轮番图
UICollectionView 用作轮番图的实现,demo 地址:https://github.com/SummerHH/YJCYCleCollectionVIew #import <UIKi ...
- Python使用selenium进行爬虫(一)
JAVA爬虫框架很多,类似JSOUP,WEBLOGIC之类的爬虫框架都十分好用,个人认为爬虫的大致思路就是: 1.挑选需求爬的URL地址,将其放入需求网络爬虫的队列,也可以把爬到的符合一定需求的地址放 ...