C#获取计算机硬件参数

计算机名称

System.Net.Dns.GetHostName();

IP地址

System.Net.IPHostEntry localhost = System.Net.Dns.GetHostByName(Dns.GetHostName());
IPAddress localaddr = localhost.AddressList[0];
this.lblIpAddress.Text = localaddr.ToString();

操作系统名称

public string GetOSystem()
        {
            RegistryKey rk;
            string OS = "";
            rk = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion");
            OS = rk.GetValue("ProductName").ToString();
            //SystemRoot = rk.GetValue("SystemRoot").ToString();
            rk.Close();
            return OS;
        }

CPU型号,主频,处理器

ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\CIMV2", "SELECT * FROM Win32_Processor");
                foreach (ManagementObject obj2 in searcher.Get())
                {
                    string result = obj2.GetPropertyValue("Name").ToString() + "," + obj2.GetPropertyValue("CurrentClockSpeed").ToString() + " Mhz"; //+                Environment.ProcessorCount.ToString() + " 个处理器");
                }

硬盘总容量

ManagementClass hardDisk = new ManagementClass("win32_DiskDrive");
                ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
                foreach (ManagementObject m in hardDiskC)
                {
                    long capacity = Convert.ToInt64(m["Size"].ToString());
                    string result = (capacity/1024/1024/1024).ToString();
                }

内存大小

private string GetMemory()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher();   //用于查询一些如系统信息的管理对象
            searcher.Query = new SelectQuery("Win32_PhysicalMemory ", "", new string[] { "Capacity" });//设置查询条件
            ManagementObjectCollection collection = searcher.Get();   //获取内存容量
            ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator();

            long capacity = 0;
            while (em.MoveNext())
            {
                ManagementBaseObject baseObj = em.Current;
                if (baseObj.Properties["Capacity"].Value != null)
                {
                    try
                    {
                        capacity += long.Parse(baseObj.Properties["Capacity"].Value.ToString());
                    }
                    catch
                    {
                        return "";
                    }
                }
            }
            return (capacity/1024/1024).ToString();
        } 

数据库版本

select @@VERSION

硬盘序列号

ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
            //Win32_DiskDrive不包含SerialNumber属性。
            ManagementObjectCollection moc = mc.GetInstances();
            string strID = null ;
            foreach( ManagementObject mo in moc )
            {   
                strID = mo.Properties["SerialNumber"].Value.ToString();
                break;
            }

是否安装了IIS服务

RegistryKey regkey = Registry.LocalMachine;
            RegistryKey sys = regkey.OpenSubKey(@"SYSTEM\CurrentControlSet\Services");
            DetailTreeNode IISNode = new DetailTreeNode();
            CheckResult += "IIS服务检查(";
            foreach (string strt in sys.GetSubKeyNames())
            {
                if (strt == "IISADMIN")

             }



你可能感兴趣的:(C#获取计算机硬件参数)