Note: The code will only work on Window 32 bit, If you are using 64 bit Operation system, you need to comment few Method GetTotalPhysicalMemory,GetTotalVirtualMemory,and GetAvailableVirtualMemory.
The MachineInfo Class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Management; using System.Net; using System.Diagnostics; namespace GenericErrorHandling { class MachineInfo { ManagementObjectSearcher query; ManagementObjectCollection result; string responseString; int responseInt; public string GetMachineName() { return Environment.MachineName.ToUpper(); } public string GetOSVersion() { return Environment.OSVersion.VersionString; } public string GetProcessorCount() { return Environment.ProcessorCount.ToString(); } public string GetIPAddress() { IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); IPAddress[] ipAddress = ipEntry.AddressList; return ipAddress[ipAddress.Length - 1].ToString(); } public string GetTotalPhysicalMemory() { query = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalMemoryConfiguration"); result = query.Get(); foreach (ManagementObject managementObject in result) { responseInt = Convert.ToInt32(managementObject["TotalPhysicalMemory"].ToString()); } responseInt = (responseInt / 1024); responseString = responseInt.ToString() + " MB"; return responseString; } public string GetAvailablePhysicalMemory() { PerformanceCounter counter = new PerformanceCounter("Memory", "Available Bytes"); responseInt = ((int)Convert.ToInt64(counter.NextValue()) * (-1)) / (1024 * 1024); responseString = responseInt.ToString() + " MB"; return responseString; } public string GetTotalVirtualMemory() { query = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalMemoryConfiguration"); result = query.Get(); foreach (ManagementObject managementObject in result) { responseInt = Convert.ToInt32(managementObject["TotalVirtualMemory"].ToString()); } responseInt = (responseInt / 1024); responseString = responseInt.ToString() + " MB"; return responseString; } public string GetAvailableVirtualMemory() { query = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalMemoryConfiguration"); result = query.Get(); foreach (ManagementObject managementObject in result) { responseInt = Convert.ToInt32(managementObject["AvailableVirtualMemory"].ToString()); } responseInt = (responseInt / 1024); responseString = responseInt.ToString() + " MB"; return responseString; } public string GetCpuFrequency() { query = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"); result = query.Get(); foreach (ManagementObject managementObject in result) { responseInt = Convert.ToInt32(managementObject["CurrentClockSpeed"].ToString()); } responseString = responseInt.ToString() + " MHz"; return responseString; } } }
The Code To Show Machine Information
private void button2_Click(object sender, EventArgs e) { MachineInfo M = new MachineInfo(); string msg = "Number Of Processor : " + M.GetProcessorCount(); msg += "\nProcessor MHZ : " + M.GetCpuFrequency(); msg += "\nMachine Name : " + M.GetMachineName(); msg += "\nGetOSVersion : " + M.GetOSVersion(); msg += "\nGetIPAddress : " + M.GetIPAddress(); msg += "\nGetTotalPhysicalMemory : " + M.GetTotalPhysicalMemory(); msg += "\nGetAvailablePhysicalMemory : " + M.GetAvailablePhysicalMemory(); msg += "\nGetTotalVirtualMemory : " + M.GetTotalVirtualMemory(); msg += "\nGetAvailableVirtualMemory : " + M.GetAvailableVirtualMemory(); MessageBox.Show(msg); }
The Output
By Mohd Zulkamal
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)
0 comments:
Post a Comment