1. 程式人生 > >c# 獲取網絡卡資訊

c# 獲取網絡卡資訊

private IList<IPInfo> GetIPInfo()
        {
            IList<IPInfo> rIPList = new List<IPInfo>();

            NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface NetworkIntf in NetworkInterfaces)
            {

                IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties();

                UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;

                foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
                {

                    if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        string IP = UnicastIPAddressInformation.Address.ToString();
                        if (IP != "127.0.0.1")
                        {
                            IPInfo info = new IPInfo();
                            info.IPAddress = IP;
                            info.OptStatus = NetworkIntf.OperationalStatus;
                            info.DisplayCaption = info.IPAddress;
                            if (info.OptStatus == OperationalStatus.Up)
                            {
                                info.DisplayCaption = info.DisplayCaption + " [已連線]";
                            }
                            else
                            {
                                info.DisplayCaption = info.DisplayCaption + " [已斷開]";
                            }
                            rIPList.Add(info);
                        }
                    }

                }

            }

            return rIPList;
        }