Example #1
0
        /// <summary>
        /// Refresh the list by calling <see cref="DeviceEnum.EnumerateNetworkDevices()"/>
        /// </summary>
        public void Refresh()
        {
            Free();

            var di = DeviceEnum.EnumerateNetworkDevices();

            _Adapters = IfDefApi.GetAdapters(ref _origPtr, true);

            foreach (var adap in _Adapters)
            {
                var newp = new NetworkAdapter(adap);

                foreach (var de in di)
                {
                    if ((de.Description ?? "") == (adap.Description ?? ""))
                    {
                        newp.DeviceInfo = de;
                        break;
                    }
                }

                _Col.Add(newp);
            }
        }
Example #2
0
        public void Refresh()
        {
            var nad  = new Dictionary <int, NetworkAdapter>();
            var lOut = new List <NetworkAdapter>();

            var newmm = new MemPtr();

            // Get the array of unmanaged IP_ADAPTER_ADDRESSES structures
            var newads = IfDefApi.GetAdapters(ref newmm, true);

            var di    = DeviceEnum.EnumerateNetworkDevices();
            var iftab = IfTable.GetIfTable();

            foreach (var adap in newads)
            {
                var newp = new NetworkAdapter(adap);
                foreach (var de in di)
                {
                    if ((de.Description ?? "") == (adap.Description ?? "") || (de.FriendlyName ?? "") == (adap.FriendlyName ?? "") || (de.FriendlyName ?? "") == (adap.Description ?? "") || (de.Description ?? "") == (adap.FriendlyName ?? ""))
                    {
                        newp.DeviceInfo = de;

                        foreach (var iface in iftab)
                        {
                            if (newp.PhysicalAddress == iface.bPhysAddr)
                            {
                                newp.PhysIfaceInternal.Add(iface);
                            }
                        }

                        nad.Add(newp.IfIndex, newp);
                        _ = Task.Run(() => PopulateInternetStatus(newp));

                        break;
                    }
                }
            }

            if (adapters == null)
            {
                adapters = new ObservableCollection <NetworkAdapter>();
            }

            if (adapters.Count == 0)
            {
                foreach (var kv in nad)
                {
                    adapters.Add(kv.Value);
                }
            }
            else
            {
                var kseen = new List <int>();

                int c = adapters.Count - 1;
                int i;

                for (i = c; i >= 0; i--)
                {
                    if (nad.ContainsKey(adapters[i].IfIndex))
                    {
                        adapters[i].AssignNewNativeObject(nad[adapters[i].IfIndex]);
                        kseen.Add(adapters[i].IfIndex);
                    }
                    else
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            adapters.RemoveAt(i);
                        });
                    }
                }

                foreach (var kv in nad)
                {
                    if (!kseen.Contains(kv.Value.IfIndex))
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            adapters.Add(kv.Value);
                        });
                    }
                }
            }

            if (_origPtr != MemPtr.Empty)
            {
                _origPtr.Free(true);
            }

            _origPtr = newmm;

            QuickSort.Sort(adapters, new Comparison <NetworkAdapter>((a, b) => a.IfIndex - b.IfIndex));
        }