一個簡單的小工具,主要用來獲取當期的電腦信息以及內存的使用情況。本來也沒想著寫這個玩意,還不如隨便下個相關的軟件好使,但是前一段時間為了追查系統(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)功能: 開發(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)效果: 由簡入繁,拿來即用
|