C#-获取磁盘,cpu,内存信息
生活随笔
收集整理的这篇文章主要介绍了
C#-获取磁盘,cpu,内存信息
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
获取磁盘信息zongdaxiao = GetHardDiskSpace("C") * 1.0 / 1024;
user = GetHardDiskFreeSpace("C") * 1.0 / 1024;///
/// 获取指定驱动器的空间总大小(单位为B)
///
/// 只需输入代表驱动器的字母即可 (大写)
///
public long GetHardDiskSpace(string str_HardDiskName)
{long totalSize = new long();str_HardDiskName = str_HardDiskName + ":\\";System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();foreach (System.IO.DriveInfo drive in drives){if (drive.Name == str_HardDiskName){totalSize = drive.TotalSize / (1024 * 1024);}}return totalSize;
}///
/// 获取指定驱动器的剩余空间总大小(单位为B)
///
/// 只需输入代表驱动器的字母即可
///
public long GetHardDiskFreeSpace(string str_HardDiskName)
{long freeSpace = new long();str_HardDiskName = str_HardDiskName + ":\\";System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();foreach (System.IO.DriveInfo drive in drives){if (drive.Name == str_HardDiskName){freeSpace = drive.TotalFreeSpace / (1024 * 1024);}}return freeSpace;
}*****************************************************
获取总内存(运行)
zongneicun = GetMemoryStatus();[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;}[DllImport("kernel32")]public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);MEMORY_INFO MemInfo = new MEMORY_INFO();private long GetMemoryStatus(){GlobalMemoryStatus(ref MemInfo);long totalMb = Convert.ToInt64(MemInfo.dwTotalPhys.ToString()) / 1024 / 1024;//long avaliableMb = Convert.ToInt64(MemInfo.dwAvailPhys.ToString()) / 1024 / 1024;return totalMb; //MessageBox.Show("物理内存共有" + totalMb.ToString () + " MB" + "可使用的物理内存有" + avaliableMb.ToString () + " MB");//Console.WriteLine("物理内存共有" + totalMb + " MB");//Console.WriteLine("可使用的物理内存有" + avaliableMb + " MB");
}******************************************************//获得pro使用内存
public int pnc()
{Process[] pro = Process.GetProcesses();double total = 0;Process temp;int i;for (i = 0; i < pro.Length; i++){temp = pro[i];total = temp.PrivateMemorySize + total;}return (int)(total / 1024 / 1024);
}
*******************************************************获取cpu使用率
PerformanceCounter PC = new PerformanceCounter("Processor", "% Processor Time", "_Total");
label1.Text = "Cpu: " + Convert.ToInt32(PC.NextValue()) + "%";*************************************************************
获取系统运行时间
label4.Text = "System Runtime: " + (Environment.TickCount / 60000).ToString();
《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读
《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读
总结
以上是生活随笔为你收集整理的C#-获取磁盘,cpu,内存信息的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: C#-播放器相关
- 下一篇: C#-Socket(TCP)