c#获取硬件资源

c#通过WMI获取硬件资源代码:

     public class GetSystemInfo
    {
        private ManagementClass mc;
        private ManagementObjectCollection moc;

        public GetSystemInfo()
        { }

        public string GetHardDiskInfo()
        {
            mc = new ManagementClass("Win32_DiskDrive");
            moc = mc.GetInstances();
            String HardDiskInfo = string.Empty;
            foreach (ManagementObject mo in moc)
            {
                HardDiskInfo = "硬盘类型:" + mo.Properties["Manufacturer"].Value.ToString() + "/n" +
                        "   硬盘型号:" + mo.Properties["Model"].Value.ToString() + "/n" +
                        "   硬盘容量:" + mo.Properties["Size"].Value.ToString() + "/n" +
                        "   硬盘序列号:" + mo.Properties["PNPDeviceID"].Value.ToString() + "/n" +
                        "   硬盘分区数:" + mo.Properties["Partitions"].Value.ToString() + "/n" +
                        "   字节/扇:" + mo.Properties["BytesPerSector"].Value.ToString() + "/n" +
                        "   扇区/道:" + mo.Properties["SectorsPerTrack"].Value.ToString() + "/n" +
                        "   磁道/族:" + mo.Properties["TracksPerCylinder"].Value.ToString() + "/n" +
                        "   总扇区:" + mo.Properties["TotalSectors"].Value.ToString() + "/n" +
                        "   总磁道:" + mo.Properties["TotalTracks"].Value.ToString() + "/n" +
                        "   总族数:" + mo.Properties["TotalCylinders"].Value.ToString() + "/n" +
                        "   总磁头:" + mo.Properties["TotalHeads"].Value.ToString();
            }
            moc = null;
            mc = null;
            return HardDiskInfo;
        }

        public string GetBIOSInfo()
        {
            mc = new ManagementClass("Win32_BIOS");
            moc = mc.GetInstances();
            String BIOSInfo = null;
            foreach (ManagementObject mo in moc)
            {
                BIOSInfo = "BIOS厂商:" + mo.Properties["Manufacturer"].Value.ToString() + "/n" +
                        "   BIOS版本:" + mo.Properties["Version"].Value.ToString();

            }
            moc = null;
            mc = null;
            return BIOSInfo;
        }

        public string GetVideoCardInfo()
        {
            mc = new ManagementClass("Win32_VideoController");
            moc = mc.GetInstances();
            String VideoCardInfo = string.Empty;
            foreach (ManagementObject mo in moc)
            {
                foreach (PropertyData pd in mo.Properties)
                {
                    if (pd.Name.Equals("Name"))
                    {
                        VideoCardInfo = VideoCardInfo + "   显卡名称:" + pd.Value + "/n";
                    }
                    else if (pd.Name.Equals("PNPDeviceID"))
                    {
                        VideoCardInfo = VideoCardInfo + "   显卡序列号:" + pd.Value + "/n";
                    }
                    else if (pd.Name.Equals("DeviceID"))
                    {
                        VideoCardInfo = VideoCardInfo + "   显卡ID:" + pd.Value + "/n";
                    }
                    else if (pd.Name.Equals("Status"))
                    {
                        VideoCardInfo = VideoCardInfo + "   显卡状态:" + pd.Value + "/n";
                    }
                    else if (pd.Name.Equals("VideoModeDescription"))
                    {
                        VideoCardInfo = VideoCardInfo + "   显卡运行模式:" + pd.Value;
                    }
                }
            }
            moc = null;
            mc = null;
            return VideoCardInfo;
        }

        public string GetUSBInfo()
        {
            mc = new ManagementClass("Win32_USBController");
            moc = mc.GetInstances();
            String USBInfo = string.Empty;
            foreach (ManagementObject mo in moc)
            {
                foreach (PropertyData pd in mo.Properties)
                {
                    if (pd.Name.Equals("Manufacturer"))
                    {
                        USBInfo = USBInfo + "   厂商:" + pd.Value + "/n";
                    }
                    else if (pd.Name.Equals("Name"))
                    {
                        USBInfo = USBInfo + "   USB控制器名称:" + pd.Value + "/n";
                    }
                    else if (pd.Name.Equals("Status"))
                    {
                        USBInfo = USBInfo + "   USB状态:" + pd.Value + "/n";
                    }
                    else if (pd.Name.Equals("PNPDeviceID"))
                    {
                        USBInfo = USBInfo + "   USB控制器ID:" + pd.Value;
                    }
                }
            }
            moc = null;
            mc = null;
            return USBInfo;
        }

        public string GetCPUInfo()
        {
            mc = new ManagementClass("Win32_Processor");
            moc = mc.GetInstances();
            String CPUInfo = string.Empty;
            foreach (ManagementObject mo in moc)
            {
                CPUInfo = "CPU频率:" + mo.Properties["currentClockSpeed"].Value.ToString() + "/n" +
                        "   CPU序号:" + mo.Properties["ProcessorID"].Value.ToString() + "/n" +
                        "   CPU主频:" + mo.Properties["ExtClock"].Value.ToString() + "/n" +
                        "   32位数据宽度:" + mo.Properties["AddressWidth"].Value.ToString() + "/n" +
                        "   32位型号描述:" + mo.Properties["Description"].Value.ToString() + "/n" +
                        "   CPU厂商:" + mo.Properties["Manufacturer"].Value.ToString();
            }
            moc = null;
            mc = null;
            return CPUInfo;
        }

        public string GetMemoryInfo()
        {
            mc = new ManagementClass("Win32_PhysicalMemory");
            moc = mc.GetInstances();
            String MemoryInfo = string.Empty;
            foreach (ManagementObject mo in moc)
            {
                MemoryInfo = "内存名:" + mo.Properties["Name"].Value.ToString() + "/n" +
                            //"   内存序列号:" + mo.Properties["PartNumber"].Value.ToString() + "/n" +
                            //"   Version:" + mo.Properties["Version"].Value.ToString() + "/n" +
                            "   TotalWidth:" + mo.Properties["TotalWidth"].Value.ToString();
            }
            moc = null;
            mc = null;
            return MemoryInfo;
        }
    }

 

参考:http://msdn.microsoft.com/en-us/library/aa394084(VS.85).aspx

        http://msdn.microsoft.com/zh-cn/library/cc437968(VS.71).aspx

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值