1. public class MonitorTools
  2. {
  3. /// <summary>
  4. /// 获取具体进程的内存,线程等参数情况
  5. /// </summary>
  6. /// <param name="processName"></param>
  7. public static void getWorkingSet(string processName)
  8. {
  9. Process[] ps = Process.GetProcesses();
  10. foreach (Process p in ps)
  11. {
  12. if (p.MainWindowHandle != null)
  13. {
  14. if (processName == p.ProcessName)
  15. {
  16. using (var process = Process.GetProcessesByName(processName)[])
  17. using (var p1 = new PerformanceCounter("Process", "Working Set - Private", processName))
  18. using (var p2 = new PerformanceCounter("Process", "Working Set", processName))
  19. {
  20. Console.WriteLine("{0}{1:N} KB", "工作集(进程类)", process.WorkingSet64 / );
  21. Console.WriteLine("{0}{1:N} KB", "内存(专用工作集)", p1.NextValue() / );
  22. Console.WriteLine(process.Threads.Count.ToString());//线程
  23. Console.WriteLine(process.Id.ToString());//PID
  24. }
  25. }
  26. }
  27. }
  28. }
  29.  
  30. /// <summary>
  31. /// 遍历获取所有硬盘的可用空间
  32. /// </summary>
  33. /// <returns></returns>
  34. public static string GetDriveInfo()
  35. {
  36. string result = string.Empty;
  37. foreach (DriveInfo item in DriveInfo.GetDrives())
  38. {
  39. if (item.DriveType == DriveType.Fixed)
  40. {
  41. result += string.Format("{0} 可用空间{1:N} \r\n", item.Name, item.AvailableFreeSpace / ( * ) + "MB");
  42. }
  43. }
  44. return result;
  45. }
  46.  
  47. /// <summary>
  48. /// 根据文件路径获得文件的大小信息
  49. /// </summary>
  50. /// <param name="filePath"></param>
  51. /// <returns></returns>
  52. public static string GetFileSize(string filePath)
  53. {
  54. string length = string.Empty;
  55. try
  56. {
  57. FileInfo file = new FileInfo(filePath);
  58. if (file != null)
  59. {
  60. length = file.Length.ToString();
  61. }
  62. return length;
  63. }
  64. catch (Exception ex)
  65. {
  66. Debug.WriteLine(ex.Message);
  67. return string.Empty;
  68. }
  69. }
  70.  
  71. //测试获取CPU,内存等运行情况
  72. [DllImport("kernel32")]
  73. public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
  74. [DllImport("kernel32")]
  75. public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
  76.  
  77. //定义CPU的信息结构
  78. [StructLayout(LayoutKind.Sequential)]
  79. public struct CPU_INFO
  80. {
  81. public uint dwOemId;
  82. public uint dwPageSize;
  83. public uint lpMinimumApplicationAddress;
  84. public uint lpMaximumApplicationAddress;
  85. public uint dwActiveProcessorMask;
  86. public uint dwNumberOfProcessors;
  87. public uint dwProcessorType;
  88. public uint dwAllocationGranularity;
  89. public uint dwProcessorLevel;
  90. public uint dwProcessorRevision;
  91. }
  92.  
  93. //定义内存的信息结构
  94. [StructLayout(LayoutKind.Sequential)]
  95. public struct MEMORY_INFO
  96. {
  97. public uint dwLength;
  98. public uint dwMemoryLoad;
  99. public uint dwTotalPhys;
  100. public uint dwAvailPhys;
  101. public uint dwTotalPageFile;
  102. public uint dwAvailPageFile;
  103. public uint dwTotalVirtual;
  104. public uint dwAvailVirtual;
  105. }
  106.  
  107. /// <summary>
  108. /// 调用GetSystemInfo函数获取CPU的相关信息
  109. /// </summary>
  110. /// <returns></returns>
  111. public static string GetCPUInfo()
  112. {
  113. try
  114. {
  115. string CPUresult = string.Empty;
  116. CPU_INFO CpuInfo;
  117. CpuInfo = new CPU_INFO();
  118. GetSystemInfo(ref CpuInfo);
  119. CPUresult += "本计算机中有" + CpuInfo.dwNumberOfProcessors.ToString() + "个CPU";
  120. CPUresult += "CPU的类型为" + CpuInfo.dwProcessorType.ToString();
  121. CPUresult += "CPU等级为" + String.Format("{0:N}", CpuInfo.dwProcessorLevel.ToString());
  122. CPUresult += "CPU的OEM ID为" + String.Format("{0:N}", CpuInfo.dwOemId.ToString());
  123. CPUresult += "CPU中的页面大小为" + String.Format("{0:N}", CpuInfo.dwPageSize.ToString());
  124.  
  125. return CPUresult;
  126. }
  127. catch (Exception ex)
  128. {
  129. Debug.WriteLine(ex.Message);
  130. return string.Empty;
  131. }
  132. }
  133.  
  134. /// <summary>
  135. /// 调用GlobalMemoryStatus函数获取内存的相关信息
  136. /// </summary>
  137. /// <returns></returns>
  138. public static string GetMemoryInfo()
  139. {
  140. try
  141. {
  142. string Memoryresult = string.Empty;
  143. MEMORY_INFO MemInfo;
  144. MemInfo = new MEMORY_INFO();
  145. GlobalMemoryStatus(ref MemInfo);
  146. Memoryresult += MemInfo.dwMemoryLoad.ToString() + "%的内存正在使用";
  147. Memoryresult += "物理内存共有" + (MemInfo.dwTotalPhys) / ( * ) + " MB";
  148. Memoryresult += "可使用的物理内存有" + (MemInfo.dwAvailPhys) / ( * ) + " MB";
  149. Memoryresult += "交换文件总大小为" + (MemInfo.dwTotalPageFile) / ( * ) + " MB";
  150. Memoryresult += "尚可交换文件大小为" + (MemInfo.dwAvailPageFile) / ( * ) + " MB";
  151. Memoryresult += "总虚拟内存有" + (MemInfo.dwTotalVirtual) / ( * ) + " MB";
  152. Memoryresult += "未用虚拟内存有" + (MemInfo.dwAvailVirtual) / ( * ) + " MB";
  153.  
  154. return Memoryresult;
  155. }
  156. catch (Exception ex)
  157. {
  158. Debug.WriteLine(ex.Message);
  159. return string.Empty;
  160. }
  161. }
  162. }
  1. /// <summary>
  2. /// 根据PID结束指定进程
  3. /// </summary>
  4. /// <param name="pid"></param>
  5. public static bool EndProcess(int pid)
  6. {
  7. try
  8. {
  9. Process process = Process.GetProcessById(pid);
  10. process.Kill();
  11. return true;
  12. }
  13. catch (Exception ex)
  14. {
  15. Debug.WriteLine(ex.Message);
  16. return false;
  17. }
  18. }
  19.  
  20. /// <summary>
  21. /// 监控线程的运行情况,如果退出,则记录退出的错误码和退出时间
  22. /// </summary>
  23. /// <param name="pid"></param>
  24. /// <returns></returns>
  25. public static string MonitorProcess(int pid)
  26. {
  27. string Message = string.Empty;
  28. try
  29. {
  30. Process process = Process.GetProcessById(pid);
  31. if (process.HasExited)
  32. {
  33. Message = string.Format("ExitCode:{0};ExitTime:{1}", process.ExitCode, process.ExitTime.ToString("yyyy年MM月dd HH时mm分ss秒"));
  34. }
  35. return Message;
  36. }
  37. catch (Exception ex)
  38. {
  39. Debug.WriteLine(ex.Message);
  40. return string.Empty;
  41. }
  42. }
  1. /// <summary>
  2. /// 调用GetSystemInfo函数获取CPU的相关信息,获取CPU占用率
  3. /// </summary>
  4. /// <returns></returns>
  5. public static string GetCPUInfo()
  6. {
  7. string CPUresult = string.Empty;
  8. try
  9. {
  10. PerformanceCounter pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
  11. pcCpuLoad.MachineName = ".";
  12. pcCpuLoad.NextValue();
  13. float cpuLoad = pcCpuLoad.NextValue();
  14. string.Format("CPU占用率:{0} %",cpuLoad);
  15. return CPUresult;
  16. }
  17. catch (Exception ex)
  18. {
  19. Debug.WriteLine(ex.Message);
  20. return string.Empty;
  21. }
  22. }

C#获取客服端ip和用户名:

  1. . asp.Net中专用属性:
  2.   获取服务器电脑名:page.server.manchinename
  3.   获取用户信息:page.user
  4.   获取客户端电脑名:page.request.userhostname
  5.   获取客户端电脑ippage.request.userhostaddress
  6.  
  7. . 在网络编程中的通用方法:
  8.   获取当前电脑名:static system.Net.dns.gethostname()
  9.   根据电脑名取出全部ip地址:static system.Net.dns.resolve(电脑名).addresslist
  10.   也可根据ip地址取出电脑名:static system.Net.dns.resolve(ip地址).hostname
  11.  
  12. . 系统环境类的通用属性:
  13.   当前电脑名:static system.environment.machinename
  14.   当前电脑所属网域:static system.environment.userdomainname
  15.   当前电脑用户:static system.environment.username
  16.  
  17. 举例子来说明:
  18.  
  19. using system.Net;
  20. private void buttonip_click(object sender, system.eventargs e)
  21. {
  22. system.Net.ipaddress[] addresslist = dns.gethostbyname(dns.gethostname()).addresslist;
  23. if (addresslist.length>)
  24. {
  25. textlip.text = addresslist[].tostring();
  26. textsip.text = addresslist[].tostring();
  27. }
  28. else
  29. {
  30. textlip.text = addresslist[].tostring();
  31. textsip.text = "没有可用的连接";
  32. }
  33. }

监控电脑CPU,内存,文件大小,硬盘空间,IP,用户名的更多相关文章

  1. Linux 性能监控之CPU&内存&I/O监控Shell脚本2

    Linux 性能监控之CPU&内存&I/O监控Shell脚本2   by:授客 QQ:1033553122 思路: 捕获数据->停止捕获数据->提取数据 备注:一些命令的输 ...

  2. Linux 性能监控之CPU&内存&I/O监控Shell脚本1

    Linux 性能监控之CPU&内存&I/O监控Shell脚本1   by:授客 QQ:1033553122   #!/bin/bash # 获取要监控的本地服务器IP地址 IP=`if ...

  3. grafana+influxdb+telegraf监控服务器cpu,内存和硬盘

    随便抄了一篇,目前我们的项目也在用,这个是linux和windows通吃的一种监控方案,非常有效,详细和优美,需要监控什么具体的业务内容,自己向influxdb中插入就行了. 监控服务器状态是运维必不 ...

  4. 使用python函数持续监控电脑cpu使用率、内存、c盘使用率等

    方法一: # import time 导入time模块 # import psutil 导入psutil模块 # def func(): # while True: ------->持续监控得w ...

  5. Mac - 苹果电脑mac系统释放硬盘空间方法汇总

    硬盘空间是大家最头痛的一个问题,大家在硬盘空间变小的时候怎么腾空间的呢?下面为大家分享7个mac系统释放空间的高级方法,大家赶紧来收了! mac系统释放硬盘空间方法: 方法一:删除Emacs--可以节 ...

  6. linux查看系统CPU,内存,硬盘使用情况

    top查看CPU,内存使用情况 free查看硬盘使用情况

  7. Linux下查看CPU型号,内存大小,硬盘空间命令

    1 查看CPU 1.1 查看CPU个数 # cat /proc/cpuinfo | grep "physical id" | uniq | wc -l 2 **uniq命令:删除重 ...

  8. Linux下查看CPU型号,内存大小,硬盘空间,进程等的命令(详解)

    转自:http://www.jb51.net/article/97157.htm 1 查看CPU 1.1 查看CPU个数 # cat /proc/cpuinfo | grep "physic ...

  9. Linux下查看CPU型号,内存大小,硬盘空间的命令

    1 查看CPU 1.1 查看CPU个数 # cat /proc/cpuinfo | grep "physical id" | uniq | wc -l 2 **uniq命令:删除重 ...

随机推荐

  1. php curl采集,服务器gzip压缩返回数据怎么办

    一般服务器不会胡乱返回gzip压缩的数据,一般是客户端请求的头部里包含你浏览器能接受的压缩方式, Accept-Encoding:gzip,deflate,sdch   这里是gzip .deflat ...

  2. 自动化测试工具 Test Studio入门教程

    Test Studio安装 可以到下载试用版 官网 http://www.telerik.com/teststudio , 装完以后需要装silverlight 安装好了,主界面是介个样子的 Test ...

  3. C 语言实例 - 输出九九乘法口诀表

    C 语言实例 - 输出九九乘法口诀表 使用嵌套 for 循环输出九九乘法口诀表. 实例 #include<stdio.h> int main(){ //外层循环变量,控制行 ; //内层循 ...

  4. linux 初始配置(centos)-网络和可视化界面

    1. 执行命令查看ip:ip addr 2. 如果没有或取到,要查看网卡是否启动,及ip配置是否正确,请先将ONBOOT改成yes,表示开机即启动 [lobin@localhost ~]$ cat / ...

  5. STP-11-多生成树:IEEE 802.1s

    IEEE802.1s多生成树(MultipleSpanningTrees,MST)有时也称为多STP(MultipleSTP,MSTP),它定义了在使用802.1QVLAN网络中,部署多实例STP的标 ...

  6. Mac OS X

    Mac OS X 除了微软自家的Windows平台, .NET Core针对Mac OS以及各种Linux(RHEL.Ubuntu.Debian.Fedora.CentOS和SUSE等)都提供了很好的 ...

  7. 我的grunt配置

    module.exports = function(grunt) { // 配置. grunt.initConfig({ pkg: grunt.file.readJSON('package.json' ...

  8. Java 8特性尝鲜:新新IO

    Java 8特性尝鲜:新新IO 在这个专题前面的文章中,我们已经看到,使用Java8的lambda表达式对现有的JDK1.2 I/O库的提升,主要是可以使用lambda表达式来构造java.io.Fi ...

  9. UIcollectionView 实现 轮番图

    UICollectionView 用作轮番图的实现,demo 地址:https://github.com/SummerHH/YJCYCleCollectionVIew #import <UIKi ...

  10. Python使用selenium进行爬虫(一)

    JAVA爬虫框架很多,类似JSOUP,WEBLOGIC之类的爬虫框架都十分好用,个人认为爬虫的大致思路就是: 1.挑选需求爬的URL地址,将其放入需求网络爬虫的队列,也可以把爬到的符合一定需求的地址放 ...