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 );
            }
        }

    }
}

相关文章推荐

Monitor Linux System Using Java-CPU&Disk&Memory&Network

Linux下Java獲取CPU、内存、磁盤IO、网络相應信息這個話題不是一個很新的東西,很多工具都做了這方面的工作。出於個人興趣,我就實作一個小工具,實現了: CPU【user】【sys 】【iowa...

Master Note For Automatic Storage Management (ASM) For Operating System (OS) Administrators [ID 1345

In this Document   Purpose   Scope   Master Note For Automatic Storage Management (ASM) For...

get CPUID for delphi

  • 2009年02月18日 11:06
  • 322KB
  • 下载

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

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

get-cpuid

  • 2006年02月23日 09:05
  • 39KB
  • 下载

Renaming a virtual machine disk (VMDK) via the vSphere Management Assistant (vMA) or vSphere CLI (vC

http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=100249...

VB源码 读取CPUID 网卡硬盘ID

  • 2012年02月12日 21:33
  • 1.09MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:using System.Management; Get CPUID,IP,MACaddress,DISK ID
举报原因:
原因补充:

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