using System.Management; Get CPUID,IP,MACaddress,DISK ID

原创 2006年05月25日 18:16:00

using System.Management;
using System;


//WMI即视窗系统管理方法(Windows Management Instrumentation),存放各种信息的数据库
namespace 使用WMI获取IP

    class WMIIP
    {  
        [STAThread]
        static void Main()
        {
            //Win32_NetworkAdapterConfiguration是WMI数据库中用来存放网络设备的信息表
            ManagementObjectSearcher query=new
                ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='TRUE'");
           
            //ManagementObjectCollection对象用来存放查询结果
            ManagementObjectCollection queryCollection=query.Get();

            foreach(ManagementObject mo in queryCollection)
            {
                Console.WriteLine("Network Card:{0}",mo["Description"]);
                Console.WriteLine("MAC Address:{0}",mo["MACAddress"]);

                //addresses, addresses和 defaultgateways 值都是可以包含多个值得数组.
                //为了适应这种情况,为了适应这种情况,用户必须把结果分配到一个字符串数组,
                //并再一次用foreach语句通过该字符串数组
                string[] addresses=(string[])mo["IPAddress"];
                string[] subnets=(string[])mo["IPSubnet"];
                string[] defaultgateways=(string[])mo["DefaultIPGateway"];

    
                //任何一个接口都可能有多个IP地址
                foreach(string ipaddress in addresses)
                {
                    Console.WriteLine("IP Address:{0}",ipaddress);
                }

                foreach(string subnet in subnets)
                {
                    Console.WriteLine("Subnet Mask:{0}",subnet);
                }

                foreach(string defaultgateway in defaultgateways)
                {
                    Console.WriteLine("Gateway:{0}",defaultgateway);
                }
                Console.WriteLine();
            }

           Console.WriteLine("*********************************");
            ManagementObjectSearcher searcher = new
                ManagementObjectSearcher("select * from win32_share");
            foreach (ManagementObject share in searcher.Get())
            {
                Console.WriteLine("Share = " + share["Name"]);
            }

        }

// Get CPU ID
        public static string GetCpuID()
        {
            try
            {
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();

                string strCpuID = null ;
                foreach( ManagementObject mo in moc )
                {
                    strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                    break;
                }
                return strCpuID;
            }
            catch
            {
                return "";
            }
        }

        // GET THE FIRST HARD DISK ID
        public static string GetHardDiskID()
        {
            ManagementClass   mc=new   ManagementClass("Win32_DiskDrive");  
            string returnstr = "";
            foreach(ManagementObject   mb   in   mc.GetInstances())  
            {
                returnstr = mb.Properties["PNPDeviceID"].Value.ToString();
                if(returnstr != "") break;
            }
            return returnstr;
        }//end


        //GET MAC ADDRESS
        public static string GetNetworkID()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
                string strNetworkID = null ;
                foreach(ManagementObject mo in searcher.Get())
                {   
                    strNetworkID = mo["MACAddress"].ToString().Trim();
                    if(strNetworkID != "")
                    {
                        break;         
                    }
                }
                return strNetworkID ;
            }
            catch
            {
                return "";
            }
        }//end

       static void SwitchToStatic()
        {
            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach( ManagementObject mo in moc )
            {
                if( ! (bool) mo[ "IPEnabled" ] )
                    continue;

                // Change current IP Address to 192.168.0.253
                inPar = mo.GetMethodParameters( "EnableStatic" );
                inPar["IPAddress"] = new string[] { "192.168.0.253" };
                inPar["SubnetMask"] = new string[] { "255.255.255.0" };
                outPar = mo.InvokeMethod( "EnableStatic", inPar, null );
                break;
            }
        }

       static void SwitchToDHCP()
        {
            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach( ManagementObject mo in moc )
            {
                if( ! (bool) mo["IPEnabled"] )
                    continue;

                inPar = mo.GetMethodParameters("EnableDHCP");
                outPar = mo.InvokeMethod( "EnableDHCP", inPar, null );
                break;
            }
        }

       static void ReportIP()
        {
            Console.WriteLine( "****** Current IP addresses:" );
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach( ManagementObject mo in moc )
            {
                if( ! (bool) mo[ "IPEnabled" ] )
                    continue;

                Console.WriteLine( "{0}/n SVC: '{1}' MAC: [{2}]", (string) mo["Caption"],
                    (string) mo["ServiceName"], (string) mo["MACAddress"] );

                string[] addresses = (string[]) mo[ "IPAddress" ];
                string[] subnets = (string[]) mo[ "IPSubnet" ];

                // display IP Addresses
                Console.WriteLine( " Addresses :" );
                foreach(string sad in addresses)
                    Console.WriteLine( "/t'{0}'", sad );

                // display SubNets mask
                Console.WriteLine( " Subnets :" );
                foreach(string sub in subnets )
                    Console.WriteLine( "/t'{0}'", sub );
            }
        }

    }
}

using System.Management; Get CPUID,IP,MACaddress,DISK ID

using System.Management; Get CPUID,IP,MACaddress,DISK ID ...
  • aiwtu
  • aiwtu
  • 2006年11月16日 20:42
  • 833

关于获取系统信息,CPUID,硬盘ID,网卡MAC地址的VS2008实现

前段时间,总是要获取一些系统的相关信息。在MSDN和各种论坛找了好久,暂时用如下办法解决了。(有网上相关的代码) 参考过使用WMI,速度不敢恭维。就使用了别的方法。 获取的包括:           ...
  • chenzy945
  • chenzy945
  • 2015年02月03日 10:36
  • 2676

linux平台取CPUID

1. 控制台通过dmidecode命令取CPUID         dmidecode -t 4 | grep ID 2. 程序中自动获取CPUID     也是通过读取dmidecod...
  • sgiwxyg
  • sgiwxyg
  • 2012年12月03日 23:25
  • 1524

更改cpuID(CPU序列号)指南

最近在使用一个软件,购买了许可。该license绑定了机器的cpuID,然该软件并不能正在运行在windows 8 64bit操作系统上,具体表现为运行3-5mins后就崩溃。于是,我计划使用VMwa...
  • vernice
  • vernice
  • 2015年05月17日 04:12
  • 20254

应用程序获得ARM的cpuid

应用程序获得ARM的cpuid
  • alien75
  • alien75
  • 2010年09月01日 00:55
  • 6713

关于android设备唯一区分device id的取得

有些apk为了区分唯一设备,需要用到一个device id。 1. 取得设备的MAC address    如果用户没有通过wifi连网路的话,就无法取得。 2. 使用TelephonyMana...
  • yuxiaohui78
  • yuxiaohui78
  • 2012年11月29日 21:05
  • 53100

vc获取CPU ID

最近要用CPU序列号,MAC地址和硬盘ID来为软件加密,在网上找了下高手写的,有很多版本,但很多不能正常运行或者达不到预期效果,这里整理下自 己采用并测试通过的 CPU序列号并不是全球唯一的,以Int...
  • Andy20081251
  • Andy20081251
  • 2014年04月01日 14:25
  • 2595

用cpuid指令获取cpu信息

用cpuid指令获取cpu信息http://blog.163.com/madengyao_super/blog/static/285982202008625105632730/ 最近在写一个获取系统信...
  • fisher_jiang
  • fisher_jiang
  • 2009年07月14日 17:22
  • 7896

汇编(指令cpuid)获取cpu信息

#include using namespace std; int main(){ unsigned int meax; unsigned int mebx; unsigned int mec...
  • xiuye2015
  • xiuye2015
  • 2017年01月03日 17:37
  • 481

Qt中获取CPU的序列号

亲测在Qt中能正确获取CPU序列号,获取序列号的代码如下:     unsigned long s1,s2,s3,s4;     char string[128];     char szCpu...
  • tercel_zhang
  • tercel_zhang
  • 2017年02月24日 13:46
  • 1782
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:using System.Management; Get CPUID,IP,MACaddress,DISK ID
举报原因:
原因补充:

(最多只允许输入30个字)