关闭

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

1843人阅读 评论(0) 收藏 举报

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

    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:335588次
    • 积分:5865
    • 等级:
    • 排名:第4501名
    • 原创:248篇
    • 转载:19篇
    • 译文:0篇
    • 评论:38条
    最新评论