/// <summary> /// Returns memory information for the physical memory. /// </summary> /// <returns>Memory status.</returns> private MemoryStatus GetApplicationMemoryStatus() { MemoryStatus ms = new MemoryStatus(); string Key = "Win32_LogicalMemoryConfiguration"; ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key); foreach (ManagementObject item in searcher.Get()) { ms.MemoryTotal = Convert.ToInt64(item["TotalPhysicalMemory"]) * 1000; // in bytes. } Key = "Win32_PerfRawData_PerfOS_Memory"; searcher = new ManagementObjectSearcher("select * from " + Key); foreach (ManagementObject item in searcher.Get()) { ms.MemoryFree = Convert.ToInt64(item["AvailableBytes"]); // in bytes. } return ms; }
/// <summary> /// Returns space infroamtion for the local disk. /// </summary> /// <returns>Memory status.</returns> private MemoryStatus GetDiskMemoryStatus() { MemoryStatus ms = new MemoryStatus(); string Key = "Win32_LogicalDisk"; ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key); foreach (ManagementObject item in searcher.Get()) { if (item["DriveType"].ToString() == "3") { // Local Disk ms.MemoryFree = Convert.ToInt64(item["FreeSpace"]); // in bytes. ms.MemoryTotal = Convert.ToInt64(item["Size"]); // in bytes. } } return ms; }
/// <summary> /// Returns TOTAL memory status information. /// </summary> /// <returns>Memory status.</returns> public override MemoryStatus GetMemoryStatus() { MemoryStatus msApplication = GetApplicationMemoryStatus(); MemoryStatus msDisk = GetDiskMemoryStatus(); #if DEBUG Console.WriteLine("Physical total memory: " + msApplication.MemoryTotal); Console.WriteLine("Physical free memory: " + msApplication.MemoryFree); Console.WriteLine("Physical used memory: " + msApplication.MemoryUsed); Console.WriteLine("Disk total space: " + msDisk.MemoryTotal); Console.WriteLine("Disk free space: " + msDisk.MemoryFree); Console.WriteLine("Disk used space: " + msDisk.MemoryUsed); #endif MemoryStatus totalMemoryStatus = new MemoryStatus(); totalMemoryStatus.MemoryTotal = msApplication.MemoryTotal + msDisk.MemoryTotal; totalMemoryStatus.MemoryFree = msApplication.MemoryFree + msDisk.MemoryFree; return totalMemoryStatus; }