Monday 1 June 2015

Fetch Machine Information C#.Net


Fetch Machine Information C#.Net

The following Code Snippet will help you to fetch the Machine/System Info like Processor Details, Machine Name, IP Address..

Create a Class File Named MachineInfo.cs:


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;
        }
    }
 
}


Then Use the following Code Snippet to get the detailed info of the target Machine:


private void button1_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);
 
        }

1 comment:

  1. What you're saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I'm sure you'll reach so many people with what you've got to say.

    ReplyDelete

Thank You for Your Comments. We will get back to you soon.

back to top