日期:2014-05-18  浏览次数:21115 次

如何让获取的多网卡的信息(IP,mac,连接状态等)
如何让获取的多网卡的信息(IP,mac,连接状态等)

------解决方案--------------------
ipconfig /all > c:\a.txt

连接状态:
Ethernet adapter 本地连接 5:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : IPSec VPN Virtual Adapter
Physical Address. . . . . . . . . : 08-00-58-00-00-05
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 10.254.0.3
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :

未连接状态:
Ethernet adapter 本地连接 5:

Media State . . . . . . . . . . . : Media disconnected
Description . . . . . . . . . . . : IPSec VPN Virtual Adapter
Physical Address. . . . . . . . . : 08-00-58-00-00-05

分析两个状态就能得到IP、MAC、状态等信息

------解决方案--------------------
做一个DOS程序。。IPCONFIG /all 然后就可以获取了。。。
------解决方案--------------------
状态Media State . . . . . . . . . . . : Media disconnected
------解决方案--------------------
C# code

本地IP地址
using System.Net;

IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName()); ;
IPAddress ipaddress = ipHost.AddressList[0];
string ips = ipaddress.ToString();

MAC地址
 string strMac = "";
 NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces)
            {
                if (ni.OperationalStatus == OperationalStatus.Up)
                {
                    strMac += ni.GetPhysicalAddress().ToString() + "|";//MAC地址
                }
            }
ni.OperationalStatus.ToString();//网络连接状态

------解决方案--------------------
C# code

ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration  where IPEnabled='True' and MACAddress = '" + MACAddress + "'");
                  ManagementObjectCollection queryCollection = query.Get();
                  foreach (ManagementObject mo in queryCollection)
                  {
                      if ((bool)mo["IPEnabled"] == true)
                      {
                          if (mo["IPAddress"] != null)
                              strIP = ((string[])mo["IPAddress"])[0];
                      }
                      else
                      {
                          strIP = "0.0.0.0";
                      }
                  }