C # to obtain information about your computer

Compiled information on a computer-related system

We need to introduce namespace:

1. In the 'Solution Explorer' window -> right-click the project -> 'Add' -> 'references' pop citation manager

2. In reference to a processor, the assembly -> frame -> select 'System.Management' -> Confirm

using System;
using System.Management;
using System.IO;
the using the System;
 the using the System.Collections.Generic;
 the using the System.Linq;
 the using the System.Text;
 the using the System.Management;
 the using the System.IO; 

namespace WindowsFormsApp1 
{ 
    ///  <Summary>  
    /// computer-based information
     ///  </ the Summary>  
    public  class GetComputerInfo 
    { 
        public  String CPUID; // Cpu number 
        public  String MacAddress; // Mac address 
        public  String DiskID; // disk ID 
        public String IpAddress; // IP address 
        public  String LoginUserName; // system user name 
        public  String ComputerName; // computer name 
        public  String SystemType; // System Type 
        public  String TotalPhysicalMemory; // Unit: M 
        Private  static GetComputerInfo _instance; 
        
        public  static GetComputerInfo GetInstance () 
        { 
            IF (_instance == null ) 
                _instance = new new GetComputerInfo ();
            return _instance;
        }
        public GetComputerInfo()
        {
            CpuID = GetCpuID();
            MacAddress = GetMacAddress();
            DiskID = GetDiskID();
            IpAddress = GetIPAddress();
            LoginUserName = GetUserName();
            SystemType = GetSystemType();
            TotalPhysicalMemory = GetTotalPhysicalMemory();
            ComputerName = GetComputerName();
        }
        /// <summary>
        /// 获取CPU的个数
        /// </summary>
        /// <returns></returns>
        public static int GetCpuCount()
        {
            try
            {
                using (ManagementClass mCpu = new ManagementClass("Win32_Processor"))
                {
                    ManagementObjectCollection cpus = mCpu.GetInstances();
                    return cpus.Count;
                }
            }
            catch
            {
            }
            return -1;
        } 

       ///  <Summary> 
       /// Get frequency of the CPU herein reason for using an array of type string, primarily because the cpu multicore
        ///  </ Summary> 
       ///  <Returns> </ Returns> 
       public  static  string [ ] GetCpuMHZ () 
        { 
            ManagementClass MC = new new ManagementClass ( " the Win32_Processor " ); 
            the ManagementObjectCollection CPUs = mc.GetInstances ();
             String [] = mHz new new  String [cpus.Count];
             int C = 0 ; 
            ManagementObjectSearcher mysearch= new ManagementObjectSearcher("select * from Win32_Processor");
            foreach (ManagementObject mo in mySearch.Get())
            {
                mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString();
                c++;
            }
            mc.Dispose();
            mySearch.Dispose();
            return mHz;
        }
        /// <summary>
        /// 获取本机硬盘的大小
        /// </summary>
        /// <returns></returns>
        public static string GetSizeOfDisk()
        {
            ManagementClass mc = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moj = mc.GetInstances();
            foreach (ManagementObject m in moj)
            {
                return m.Properties["Size"].Value.ToString();
            }
            return "-1";
        }
        /// <summary>
        ///  获取本机内存的大小:
        /// </summary>
        /// <returns></returns>
        public static string GetSizeOfMemery()
        {
            ManagementClass mc = new ManagementClass("Win32_OperatingSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            double sizeAll = 0.0;
            foreach (ManagementObject m in moc)
            {
                if (m.Properties["TotalVisibleMemorySize"].Value != null)
                {
                    sizeAll += Convert.ToDouble(m.Properties["TotalVisibleMemorySize"].Value.ToString());
                }
            }
            mc = null;
            moc.Dispose();
            return sizeAll.ToString();
        }
      
        /// <summary>
        /// 获取磁盘剩余空间
        /// </summary>
        /// <param name="str_HardDiskName"></param>
        /// <returns></returns>
        long GetHardDiskFreeSpace(string str_HardDiskName)
        {
            long num = 0L;
            str_HardDiskName = str_HardDiskName + @":\";
            foreach (DriveInfo info in DriveInfo.GetDrives())
            {
                if (info.Name.ToUpper() == str_HardDiskName.ToUpper())
                {
                    num = info.TotalFreeSpace / 0x100000L;
                }
            }
            return num;
        }

        //获得CPU编号
        string GetCpuID()
        {
            the try 
            { 
                //Get CPU serial number code 
                String cpuinfo = "" ; // CPU serial number 
                ManagementClass MC = new new ManagementClass ( " the Win32_Processor " ); 
                the ManagementObjectCollection MOC = mc.GetInstances ();
                 the foreach (Mo ManagementObject in MOC) 
                { 
                    cpuinfo = mo.Properties [ " ProcessorId " ] .Value.ToString (); 
                } 
                MOC = null ; 
                MC = null ;
                 return cpuinfo; 
            } 
            the catch 
            { 
                return  " unknow " ; 
            } 
        } 
        // get the Mac address 
        String getmacaddress () 
        { 
            the try 
            { 
                // Get the NIC hardware address 
                String MAC = "" ; 
                ManagementClass MC = new new ManagementClass ( " the Win32_NetworkAdapterConfiguration " ); 
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        mac = mo["MacAddress"].ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return mac;
            }
            catch
            {
                return "unknow";
            }
        }
        //获得Ip地址
        string GetIPAddress()
        {
            try
            {
                //获取IP地址 
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        //st=mo["IpAddress"].ToString(); 
                        System.Array ar;
                        ar = (System.Array)(mo.Properties["IpAddress"].Value);
                        st = ar.GetValue(0).ToString();
                        break;
                    }
                }
                moc = null;
                MC = null ;
                 return ST; 
            } 
            the catch 
            { 
                return  " unknow " ; 
            } 
        } 
        // get the disk Id 
        String GetDiskID () 
        { 
            the try 
            { 
                // Get hard ID 
                String HDid = "" ; 
                ManagementClass MC = new new ManagementClass ( " Win32_DiskDrive " ); 
                moc ManagementObjectCollection = mc.GetInstances();
                the foreach (Mo ManagementObject in MOC) 
                { 
                    HDid = ( String ) mo.Properties [ " the Model " ] .Value; 
                } 
                MOC = null ; 
                MC = null ;
                 return HDid; 
            } 
            the catch 
            { 
                return  " unknow " ; 
            } 
        } 
        ///  < the Summary>  
        /// operating system login user name 
         ///  </ Summary>  
        ///  <Returns> </ Returns>  
        String the GetUserName () 
        { 
            the try 
            { 
                String ST = "" ; 
                ManagementClass MC = new new ManagementClass ( " the Win32_ComputerSystem " ); 
                the ManagementObjectCollection MOC = mc.GetInstances ();
                 the foreach (Mo ManagementObject in MOC) 
                { 
                    ST = Mo [ " UserName " ] .ToString ();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
        }
        /// <summary> 
        /// PC类型 
        /// </summary> 
        /// <returns></returns> 
        string GetSystemType()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = mo["SystemType"].ToString();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary> 
        /// 物理内存 
        /// </summary> 
        /// <returns></returns> 
        string GetTotalPhysicalMemory()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
           st = mo["TotalPhysicalMemory"].ToString();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
        }
        /// <summary> 
        ///  获取计算机名称
        /// </summary> 
        /// <returns></returns> 
        string GetComputerName()
        {
            try
            {
                return System.Environment.GetEnvironmentVariable("ComputerName");
            }
            catch
            {
                return "unknow";
            }
        }
    }
}

 ok, today to share this, and if in doubt can leave a message, not to speak of the welcome that! ! !

Guess you like

Origin www.cnblogs.com/guhuazhen/p/11114680.html