Beispiel #1
0
 private static String _GetMacAddress()
 {
     if (_cachedDeviceUniqueId == null)
     {
         _cachedDeviceUniqueId = NetWorkInfo.GetMacAddress();
     }
     return(_cachedDeviceUniqueId);
 }
        /// <summary>
        /// Check Current NewWork is Avaliable
        /// </summary>
        /// <returns>Is Connect To NetWork</returns>
        public NetWorkInfo CheckNewWorkIsAvailable()
        {
            bool        isConnectNet       = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
            bool        isWifi             = DeviceNetworkInformation.IsWiFiEnabled;
            NetWorkInfo currentNetWorkInfo = new NetWorkInfo()
            {
                IsConnectNetWork = isConnectNet, IsWifi = isWifi
            };

            IsolatedStorageHelper.IsolatedStorageSaveObject(_netWorkSaveKey, currentNetWorkInfo);

            NetworkChange.NetworkAddressChanged += (sender, ex) =>
            {
                NetWorkInfo changeNetWork = new NetWorkInfo()
                {
                    IsConnectNetWork = DeviceNetworkInformation.IsNetworkAvailable,
                    IsWifi           = DeviceNetworkInformation.IsWiFiEnabled
                };
                IsolatedStorageHelper.IsolatedStorageSaveObject(_netWorkSaveKey, changeNetWork);
            };
            return(currentNetWorkInfo);
        }
Beispiel #3
0
        public static NetWorkInfo GetNetWorkInfoForAdapter(string adapterName)
        {
            NetWorkInfo nwi = null;

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                //太网连接
                bool isEthernet = (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet);
                //无线连接
                bool isWireless = (adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
                Console.WriteLine("Name:" + adapter.Name + " Description:" + adapter.Description +
                                  " isEthernet:" + isEthernet + " isWireless:" + isWireless);
                if ((isEthernet || isWireless) && adapter.Description.Equals(adapterName))
                {
                    nwi      = new NetWorkInfo();
                    nwi.Name = adapter.Name;
                    if (isEthernet)
                    {
                        if (adapter.Description.ToLower().IndexOf("bluetooth") > -1)
                        {
                            nwi.Type = "Bluetooth Ethernet";
                        }
                        else
                        {
                            nwi.Type = "Gigabit Ethernet";
                        }
                    }
                    if (isWireless)
                    {
                        nwi.Type = "802.11 Wireless Ethernet";
                    }
                    IPInterfaceProperties ip = adapter.GetIPProperties();
                    if (ip.UnicastAddresses.Count > 0)
                    {
                        foreach (var item in ip.UnicastAddresses)
                        {
                            if (item.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                            {
                                nwi.Ip   = item.Address.ToString();
                                nwi.Mask = item.IPv4Mask.ToString();
                            }
                        }
                    }
                    if (ip.GatewayAddresses.Count > 0)
                    {
                        foreach (var item in ip.GatewayAddresses)
                        {
                            if (item.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                            {
                                nwi.GateWay = item.Address.ToString();
                            }
                        }
                    }
                    int DnsCount = ip.DnsAddresses.Count;
                    if (DnsCount == 1)
                    {
                        nwi.DNS1 = ip.DnsAddresses[0].ToString();
                    }
                    if (DnsCount == 2)
                    {
                        nwi.DNS1 = ip.DnsAddresses[0].ToString();
                        nwi.DNS2 = ip.DnsAddresses[1].ToString();
                    }
                }
            }
            return(nwi);
        }
Beispiel #4
0
        public static List <NetWork> GetNetWorkAdpterInfo(string adapter)
        {
            try
            {
                List <NetWork> net_list_info = new List <NetWork>();

                ManagementClass            mc  = new ManagementClass("Win32_NetworkAdapter");
                ManagementObjectCollection moc = mc.GetInstances();
                String name = String.Empty;

                foreach (ManagementObject m in moc)
                {
                    try
                    {
                        name = m.Properties["Name"].Value.ToString();
                        if (Boolean.Parse(m.Properties["PhysicalAdapter"].Value.ToString()) == true)
                        {
                            if (adapter.Equals(name))
                            {
                                NetWorkInfo nwi = GetNetWorkInfoForAdapter(adapter);

                                net_list_info.Add(new NetWork("网络适配器信息", "..\\Resources\\ethernet_32px.png"));
                                net_list_info.Add(new NetWork("网络适配器", "..\\Resources\\ethernet_32px.png", name));
                                if (nwi != null)
                                {
                                    net_list_info.Add(new NetWork("接口类型", "..\\Resources\\ethernet_32px.png", nwi.Type));
                                    net_list_info.Add(new NetWork("硬件地址(MAC)", "..\\Resources\\ethernet_32px.png", m.Properties["MACAddress"].Value.ToString()));
                                    net_list_info.Add(new NetWork("连接名称", "..\\Resources\\ethernet_32px.png", nwi.Name));

                                    int    flag   = Convert.ToUInt16(m.Properties["NetConnectionStatus"].Value);
                                    string status = "已断开";
                                    if (flag == 2)
                                    {
                                        status = "已连接";
                                    }
                                    net_list_info.Add(new NetWork("连接状态", "..\\Resources\\ethernet_32px.png", status));

                                    net_list_info.Add(new NetWork("网络适配器地址", "..\\Resources\\ethernet_32px.png"));
                                    if (!string.IsNullOrEmpty(nwi.Ip) && nwi.Ip.Length > 0)
                                    {
                                        net_list_info.Add(new NetWork("IP 地址", "..\\Resources\\ethernet_32px.png", nwi.Ip));
                                    }
                                    if (!string.IsNullOrEmpty(nwi.Mask) && nwi.Mask.Length > 0)
                                    {
                                        net_list_info.Add(new NetWork("子网掩码", "..\\Resources\\ethernet_32px.png", nwi.Mask));
                                    }
                                    if (!string.IsNullOrEmpty(nwi.GateWay) && nwi.GateWay.Length > 0)
                                    {
                                        net_list_info.Add(new NetWork("网关地址", "..\\Resources\\ethernet_32px.png", nwi.GateWay));
                                    }
                                    if (!string.IsNullOrEmpty(nwi.DNS1) && nwi.DNS1.Length > 0)
                                    {
                                        net_list_info.Add(new NetWork("主DNS地址", "..\\Resources\\ethernet_32px.png", nwi.DNS1));
                                    }
                                    if (!string.IsNullOrEmpty(nwi.DNS2) && nwi.DNS2.Length > 0)
                                    {
                                        net_list_info.Add(new NetWork("备用DNS地址", "..\\Resources\\ethernet_32px.png", nwi.DNS2));
                                    }
                                }


                                net_list_info.Add(new NetWork("网络适配器制造商", "..\\Resources\\ethernet_32px.png"));
                                string Manufacturer = m.Properties["Manufacturer"].Value.ToString();
                                net_list_info.Add(new NetWork("公司名称", "..\\Resources\\ethernet_32px.png", Manufacturer));
                                if (Manufacturer.ToLower().IndexOf("vmware") > -1)
                                {
                                    net_list_info.Add(new NetWork("产品信息", "..\\Resources\\ethernet_32px.png", "https://www.vmware.com"));
                                    net_list_info.Add(new NetWork("驱动程序下载", "..\\Resources\\ethernet_32px.png", "https://www.vmware.com"));
                                }
                                else if (Manufacturer.ToLower().IndexOf("virtual") > -1)
                                {
                                    net_list_info.Add(new NetWork("产品信息", "..\\Resources\\ethernet_32px.png", "https://www.virtualbox.org"));
                                    net_list_info.Add(new NetWork("驱动程序下载", "..\\Resources\\ethernet_32px.png", "https://www.virtualbox.org"));
                                }
                                else if (Manufacturer.ToLower().IndexOf("intel") > -1)
                                {
                                    net_list_info.Add(new NetWork("产品信息", "..\\Resources\\ethernet_32px.png", "https://www.intel.com/content/www/us/en/products/network-io.html"));
                                    net_list_info.Add(new NetWork("驱动程序下载", "..\\Resources\\ethernet_32px.png", "https://www.intel.com/support/network"));
                                }
                                else if (Manufacturer.ToLower().IndexOf("realtek") > -1)
                                {
                                    net_list_info.Add(new NetWork("产品信息", "..\\Resources\\ethernet_32px.png", "https://www.realtek.com/products"));
                                    net_list_info.Add(new NetWork("驱动程序下载", "..\\Resources\\ethernet_32px.png", "https://www.realtek.com/downloads"));
                                }
                                else
                                {
                                    //net_list_info.Add(new NetWork("产品信息", "..\\Resources\\ethernet_32px.png", "unknow"));
                                    //net_list_info.Add(new NetWork("驱动程序下载", "..\\Resources\\ethernet_32px.png", "unknow"));
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }

                return(net_list_info);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Beispiel #5
0
        //网络适配器
        public static List <BaseData> GetNetWorkInfo()
        {
            try
            {
                List <BaseData>            net_list = new List <BaseData>();
                ManagementClass            mc       = new ManagementClass("Win32_NetworkAdapter");
                ManagementObjectCollection moc      = mc.GetInstances();
                String Name       = String.Empty;
                String MACAddress = String.Empty;
                string type       = String.Empty;
                int    flag       = 0;
                string status     = String.Empty;
                bool   netEnabled = false;
                string temp       = String.Empty;
                foreach (ManagementObject m in moc)
                {
                    try
                    {
                        type = m.Properties["PNPDeviceID"].Value.ToString();
                    }
                    catch (Exception)
                    {
                    }

                    temp += type + " " + m.Properties["PhysicalAdapter"].Value.ToString() + "\r\n";
                    Console.WriteLine(type + " " + m.Properties["PhysicalAdapter"].Value.ToString());

                    if (type.Length > 3 &&
                        (type.Substring(0, 3) == "PCI" ||
                         type.Substring(0, 3) == "USB" ||
                         type.Substring(0, 3) == "BTH") &&
                        Boolean.Parse(m.Properties["PhysicalAdapter"].Value.ToString()))
                    {
                        try
                        {
                            Name       = m.Properties["Name"].Value.ToString();
                            MACAddress = m.Properties["MACAddress"].Value.ToString();
                            flag       = Convert.ToUInt16(m.Properties["NetConnectionStatus"].Value);
                            netEnabled = Convert.ToBoolean(m.Properties["NetEnabled"].Value);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                        NetWorkInfo nwi = GetNetWorkInfoForAdapter(Name);
                        if (nwi != null)
                        {
                            if (type.Substring(0, 3) == "PCI")
                            {
                                if (nwi.Type.ToLower().IndexOf("wireless") > -1)
                                {
                                    net_list.Add(new BaseData("PCI无线网卡", "..\\Resources\\WIFI_32px.png", 0));
                                }
                                else
                                {
                                    net_list.Add(new BaseData("PCI有线网卡", "..\\Resources\\Network.png", 0));
                                }
                            }
                            else if (type.Substring(0, 3) == "USB")
                            {
                                if (nwi.Type.ToLower().IndexOf("wireless") > -1)
                                {
                                    net_list.Add(new BaseData("USB无线网卡", "..\\Resources\\WIFI_32px.png", 0));
                                }
                                else
                                {
                                    net_list.Add(new BaseData("USB有线网卡", "..\\Resources\\Network.png", 0));
                                }
                            }
                            else if (type.Substring(0, 3) == "BTH")
                            {
                                net_list.Add(new BaseData("蓝牙适配器", "..\\Resources\\Bluetooth_32px.png", 0));
                            }

                            net_list.Add(new BaseData("适配器", Name));
                            net_list.Add(new BaseData("MAC", MACAddress));

                            switch (flag)
                            {
                            case 2:
                                status = "已连接";
                                break;

                            case 7:
                                status = "已断开";
                                break;

                            default:
                                status = "已断开";
                                break;
                            }
                            net_list.Add(new BaseData("状态", status));

                            if (netEnabled)
                            {
                                net_list.Add(new BaseData("IPv4地址", nwi.Ip));
                            }
                        }
                    }
                }
                //MessageBox.Show(temp);

                mc = null;
                moc.Dispose();
                return(net_list);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(null);
            }
        }