读取硬件信息的参考代码

 using System;
using System.Management;

namespace HTSoft.Common.Register
{
    /// <summary>
    /// 计算机信息类
    /// </summary>
    internal class Computer
    {
        public string CpuID;
        public string MacAddress;
        public string DiskID;
        public string IpAddress;
        public string LoginUserName;
        public string ComputerName;
        public string SystemType;
        public string TotalPhysicalMemory; //单位:M
        private static Computer _instance;

        internal static Computer Instance()
        {
            if (_instance == null)
                _instance = new Computer();
            return _instance;
        }

        internal Computer()
        {
            CpuID = GetCpuID();
            MacAddress = GetMacAddress();
            DiskID = GetDiskID();
            IpAddress = GetIPAddress();
            LoginUserName = GetUserName();
            SystemType = GetSystemType();
            TotalPhysicalMemory = GetTotalPhysicalMemory();
            ComputerName = GetComputerName();
        }
        string GetCpuID()
        {
            try
            {
                //获取CPU序列号代码
                string cpuInfo = "";//cpu序列号
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }
        string GetMacAddress()
        {
            try
            {
                //获取网卡硬件地址
                string mac = "";
                ManagementClass mc = new ManagementClass("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";
            }
            finally
            {
            }

        }
        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;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        string GetDiskID()
        {
            try
            {
                //获取硬盘ID
                String HDid = "";
                ManagementClass mc = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                moc = null;
                mc = null;
                return HDid;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        /// <summary>
        /// 操作系统的登录用户名
        /// </summary>
        /// <returns></returns>
        string GetUserName()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {

                    st = mo["UserName"].ToString();

                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        /// <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";
            }
            finally
            {
            }

        }

        /// <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";
            }
            finally
            {
            }
        }
        /// <summary>
        ///  获取计算机名称
        /// </summary>
        /// <returns></returns>
        string GetComputerName()
        {
            try
            {
                return System.Environment.GetEnvironmentVariable("ComputerName");
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }
    }
}

时间: 2024-09-30 21:56:18

读取硬件信息的参考代码的相关文章

显示系统信息与硬件信息的bat代码_DOS/BAT

显示系统信息的dos命令,使用批处理显示硬件信息的批处理 这个得看你需要查看的方式. 1. cmd窗口查看 2. 程序窗口查看 3. 文本查看 方法1. cmd窗口查看: @echo off systeminfo dxdiag /dontskip /whql:off /t systeminfo1.txt&&more systeminfo1.txt pause 方法2. 程序窗口查看: @echo off msinfo32 dxdiag pause 方法3. 文本查看: @echo off

Hardware_Info.vbs 获取硬件信息的VBS代码_vbs

复制代码 代码如下: 'Hardware_Info.vbs v1.1 BY: fastslzOn Error Resume NextDim WMI,WS,FsoSet WMI = GetObject("Winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")Set cOSs = WMI.ExecQuery("Select * from Win32_OperatingSystem")For Each oOS

asp.net 获取机器硬件信息(cpu频率、磁盘可用空间、内存容量等)_实用技巧

复制代码 代码如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; usi

用C#获取电脑硬件信息

现在我将来解释如何在C#中用WMI(Windows Management Instrumentation)来获取电脑的硬件信息. 我们的目的是用WMI的api在C#下获取电脑的以下信息: 物理处理器的数量 逻辑处理器的数量 位数 系统构架 内核数量 在Visual Studio中创建一个控制台应用程序,并且右击引用然后选择"添加引用",再选择"System.Management". 现在已经通过using语句包括了System.Management,你可以在你的代码

ldap-怎样读取ActiveDirectory域中的所有电脑的硬件信息

问题描述 怎样读取ActiveDirectory域中的所有电脑的硬件信息 一般情况都是使用LDAP读取AD服务器中用户信息,现在公司需要实现把域中所有电脑的硬件信息(例如硬盘.cpu型号.内存大小)搜集起来,不知道谁有没有过这方面的经验,给点意见,谢谢! 解决方案 On Error Resume Nexttemp=0set wshshell=wscript.createobject(""wscript.shell"")wshshell.run ("&quo

菜鸟求帮忙 php读取文本信息怎么只读取了一部分 代码和图都在

问题描述 菜鸟求帮忙 php读取文本信息怎么只读取了一部分 代码和图都在 求大神帮帮忙,帮我看看 到底是什么问题 解决方案 因为你的文件中有"<",php输出的内容会被当做html来解析,然后你文件中的<z就被当做html来解析了,所以就没有被输出,解决办法: echo htmlentities($html); 解决方案二: 有多余的标点符号,.

java-Java远程连接操作服务器,并取得服务器的端口,内存硬件等信息(求代码)

问题描述 Java远程连接操作服务器,并取得服务器的端口,内存硬件等信息(求代码) 请教高手: 用Java实现远程连接操作服务器(知道服务器的IP,账户和密码),对服务器进行关闭和重启操作,并可以获得服务器的端口状态,内存,CPU,硬件,操作系统的信息. 解决方案 Java远程配置的代码如下: import java.net.*; import java.io.*; public class Server { ServerSocket server=null; Socket you=null;

windows linux—unix 跨平台通信集成控制系统----系统硬件信息获取

控制集成系统需要了解系统的各项硬件信息,之前我们设计的时候,习惯使用c函数来搞,后来可能发现程序的移植性收到了一些影响,比如unix内核的一些c函数在linux下面是没有的: 比如   苹果达尔文内核的如下东西,linux里面就没有:    //kern_return_t kr;     //host_name_port_t myhost;    // kernel_version_t kversion;    // host_basic_info_data_t hinfo;    // mac

python获取各操作系统硬件信息的方法

  本文实例讲述了python获取各操作系统硬件信息的方法.分享给大家供大家参考.具体如下: 1. windows 使用WMI: (WMI官网地址:http://pypi.python.org/pypi/WMI 或 点击此处本站下载.) ? 1 2 3 4 5 import wmi w=wmi.WMI() cpus=w.Win32_Processor() for u in cpus: print 'cpu id:',u.ProcessorId 运行结果如下: ? 1 2 cpu id: BFEB