一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

C# 獲取系統(tǒng)使用情況

 Csharp小記 2023-10-29 發(fā)布于江蘇

    一個簡單的小工具,主要用來獲取當期的電腦信息以及內存的使用情況。本來也沒想著寫這個玩意,還不如隨便下個相關的軟件好使,但是前一段時間為了追查系統(tǒng)卡頓的問題,所以就加上了一段統(tǒng)計內存的代碼,用來看看是否由這個引起的。

既然統(tǒng)計了內存相關的數(shù)據,干脆一不做二不休,又加了下CPU以及硬盤的使用情況統(tǒng)計。

統(tǒng)計的準確不準確我也不好說,因為我也不太能確定,暫且就做個參考值吧。或許使用很多大佬封裝過的第三方DLL會準確很多。

下面工具中還展示了部分關于電腦的基本信息數(shù)據,這個在之前的時候應該是有發(fā)過,我只是單純的拿了過來(功能實現(xiàn)的多看著洋氣)。

主要還是使用windows WMI,雖然下列關于內存情況的統(tǒng)計沒用(但其實也可以統(tǒng)計到),因為我感覺這個方式可能稍微準確點。。。

實現(xiàn)功能:

    • 獲取系統(tǒng)內存、CPU的使用情況

開發(fā)環(huán)境:

開發(fā)工具:Visual Studio 2013

.NET Framework版本:4.5

實現(xiàn)代碼:

 public class SystemUtil    {        #region 內存
#region 獲得內存信息API [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GlobalMemoryStatusEx(ref MEMORY_INFO mi); //定義內存的信息結構 [StructLayout(LayoutKind.Sequential)] private struct MEMORY_INFO { public uint DWLength; //當前結構體大小 public uint DWMemoryLoad; //當前內存使用率 public ulong ullTotalPhys; //總計物理內存大小 public ulong ullAvailPhys; //可用物理內存大小 public ulong ullTotalPagefile; //總計交換文件大小 public ulong ullAvailPagefile; //總計交換文件大小 public ulong ullTotalVirtual; //總計虛擬內存大小 public ulong ullAvailVirtual; //可用虛擬內存大小 public ulong ullAvailExtendedVirtual; //保留 這個值始終為0 }
private static MEMORY_INFO GetMemoryInfo() { MEMORY_INFO MemoryInfo = new MEMORY_INFO(); MemoryInfo.DWLength = (uint)System.Runtime.InteropServices.Marshal.SizeOf(MemoryInfo); GlobalMemoryStatusEx(ref MemoryInfo); return MemoryInfo; } #endregion
/// <summary> /// 獲取系統(tǒng)內存 /// </summary> /// <returns></returns> public static string GetSysMemory() { MEMORY_INFO MemoryInfo = GetMemoryInfo(); return ConvertBytes((long)MemoryInfo.ullTotalPhys); }
/// <summary> /// 獲取剩余內存 /// </summary> /// <returns></returns> public static string GetUnUsedMemory() { MEMORY_INFO MemoryInfo = GetMemoryInfo(); return ConvertBytes((long)MemoryInfo.ullAvailPhys); }

/// <summary> /// 獲取已使用內存 /// </summary> /// <returns></returns> public static string GetUsedMemory() { MEMORY_INFO MemoryInfo = GetMemoryInfo();
return ConvertBytes((long)(MemoryInfo.ullTotalPhys - MemoryInfo.ullAvailPhys)); }

/// <summary> /// 獲取應用使用內存 /// </summary> /// <param name="appName"></param> /// <returns></returns> public static string GetAppMemory(string appName) { Process CurrentProcess = Process.GetProcessesByName(appName)[0]; PerformanceCounter Perform = new PerformanceCounter("Process", "Working Set - Private", CurrentProcess.ProcessName); return ConvertBytes((long)Perform.NextValue()); }
/// <summary> /// 獲取當前應用使用內存 /// </summary> /// <returns></returns> public static string GetCurrentAppMemory() { long memory = Process.GetCurrentProcess().PrivateMemorySize64; return ConvertBytes(memory); }

#endregion
#region CPU /// <summary> /// 獲取CPU使用率 /// </summary> /// <returns></returns> public static string GetUsedCPU() { ManagementClass mc = new ManagementClass("Win32_PerfFormattedData_PerfOS_Processor"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>();
foreach (ManagementObject mo in moc) { if (mo["Name"].ToString() == "_Total") { list.Add(mo["PercentProcessorTime"].ToString()); } } return list.Sum(s => int.Parse(s)) + "%"; } #endregion
#region 硬盤 public static string GetUsedDisk() { ManagementClass mc = new ManagementClass("Win32_PerfFormattedData_PerfDisk_PhysicalDisk"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>();
foreach (ManagementObject mo in moc) { if (mo["Name"].ToString() == "_Total") { list.Add(mo["PercentDiskTime"].ToString()); } } return list.Sum(s => int.Parse(s)) + "%"; }
#endregion
#region 電腦信息 /// <summary> /// 獲取CPU序列號 /// </summary> /// <returns></returns> public static string GetCpuID() { ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo.Properties["ProcessorId"].Value.ToString()); } return string.Join("|", list);
//若需要獲取所有屬性,可迭代ManagementObject.Properties //foreach (PropertyData pd in mo.Properties) //{ // Console.WriteLine(pd.Name + "----" + pd.Value); //} }
/// <summary> /// 獲取Mac地址 /// </summary> /// <returns></returns> public static string GetMac() { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { list.Add(mo["MacAddress"].ToString()); } } return string.Join("|", list); }
/// <summary> /// 獲取IP地址 /// </summary> /// <returns></returns> public static string GetIp() { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { list.Add(((Array)mo.Properties["IpAddress"].Value).GetValue(0).ToString()); } } return string.Join("|", list); }
/// <summary> /// 獲取硬盤序列號 /// </summary> /// <returns></returns> public static string GetDiskID() { ManagementClass mc = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo.Properties["Model"].Value.ToString()); } return string.Join("|", list); }
/// <summary> /// 獲取系統(tǒng)名稱 /// </summary> /// <returns></returns> public static string GetSystemName() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["Name"].ToString()); } return string.Join("|", list); // return System.Environment.GetEnvironmentVariable("ComputerName") }
/// <summary> /// 獲取當前登錄用戶 /// </summary> /// <returns></returns> public static string GetLoginUser() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["UserName"].ToString()); } return string.Join("|", list); }
/// <summary> /// 獲取電腦類型 /// </summary> /// <returns></returns> public static string GetPcType() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["SystemType"].ToString()); } return string.Join("|", list); }
/// <summary> /// 獲取系統(tǒng)內存 /// </summary> /// <returns></returns> public static string GetSysMemory2() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["TotalPhysicalMemory"].ToString()); } return string.Join("|", list); }
/// <summary> /// 獲取剩余內存 /// </summary> /// <returns></returns> public static string GetUnUsedMemory2() { ManagementClass mc = new ManagementClass("Win32_PerfFormattedData_PerfOS_Memory"); ManagementObjectCollection moc = mc.GetInstances(); long memory = 0; foreach (ManagementObject mo in moc) { memory += long.Parse(mo.Properties["AvailableMBytes"].Value.ToString()); } return ConvertBytes(memory); }
/// <summary> /// 獲取電腦品牌 /// </summary> /// <returns></returns> public static string GetPcBrand() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["Manufacturer"].ToString()); } return string.Join("|", list); }
/// <summary> /// 獲取電腦型號 /// </summary> /// <returns></returns> public static string GetPcModel() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["SystemFamily"].ToString()); } return string.Join("|", list); } #endregion
public static string ConvertBytes(long len) { double dlen = len; string[] sizes = { "B", "KB", "MB", "GB", "TB" }; int order = 0; while (dlen >= 1024 && order + 1 < sizes.Length) { order++; dlen = dlen / 1024; } return String.Format("{0:0.##} {1}", dlen, sizes[order]); } }

實現(xiàn)效果:

由簡入繁,拿來即用

    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    国产精品一区二区视频大全| 91日韩在线视频观看| 欧美黄色黑人一区二区| 亚洲视频一区自拍偷拍另类| 日韩高清毛片免费观看| 暴力三级a特黄在线观看| 久久福利视频这里有精品| 中文字幕一区二区三区中文| 视频一区日韩经典中文字幕| 日本道播放一区二区三区| 91福利视频日本免费看看| 99久久国产精品亚洲| 久久99青青精品免费观看| 中文字幕亚洲人妻在线视频| 亚洲一区二区三区中文久久| 丁香七月啪啪激情综合| 国产一级内射麻豆91| 日韩欧美黄色一级视频| 欧美日韩精品视频在线| 偷拍偷窥女厕一区二区视频| 久久精品亚洲精品一区| 麻豆在线观看一区二区| 男人操女人下面国产剧情| 四季精品人妻av一区二区三区| 少妇人妻精品一区二区三区| 欧美午夜不卡在线观看| 欧美日韩综合在线精品| 成人国产激情福利久久| 国产色第一区不卡高清| 国产精品人妻熟女毛片av久久| 国产成人精品久久二区二区| 精品少妇人妻av免费看| 大尺度激情福利视频在线观看| 国产自拍欧美日韩在线观看 | 国产传媒高清视频在线| 五月婷婷综合缴情六月| 国产精品免费不卡视频| 国产又粗又爽又猛又黄的| 日韩精品人妻少妇一区二区| 亚洲精品一区二区三区日韩| 91精品国产综合久久不卡|