private void NetworkWatcher_NetworkEvent(object sender, EventArgs e) { if (this.InvokeRequired) { this.Invoke(new EventHandler((o, e1) => NetworkWatcher_NetworkEvent(sender, e))); return; } DelayedExecute(10000, () => { // update proxy drop down items and apply default on all network events var ni = NetworkInterfaceInternal.GetTransmittingInterfaces() .Select(n => new NetworkConnectionInfo(n)) .Where(i => i.InitializedSuccessfully) .ToArray(); // check if the elements in the two sets are different before applying setting // prevents unnecessary updates to settings and hence balloon pop ups. if (!ni.ElementsAreSame(transmittingInterfaces)) { transmittingInterfaces = ni; var matches = FindNetworkMatches(ni, 0.5); if (matches.Any()) { ApplyNetworkSetting(matches.First().NetworkSetting.Configuration); } UpdateProxyDropDownItems(); } }, "UpdateProxy"); }
static PropertyNames() { var n = new NetworkInterfaceInternal(null); Id = GetPropertyName(() => n.Id); InterfaceType = GetPropertyName(() => n.InterfaceType); MacAddress = GetPropertyName(() => n.MacAddress); Name = GetPropertyName(() => n.Name); PhysicalAddress = GetPropertyName(() => n.PhysicalAddress); Description = GetPropertyName(() => n.Description); }
/// <summary>Reads the JSON representation of the object.</summary> /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var ni = new NetworkInterfaceInternal(null); while (reader.Read()) { if (reader.TokenType == JsonToken.EndObject) { break; } if (reader.TokenType != JsonToken.PropertyName) { continue; } var propName = reader.Value.ToString(); // property name if (propName == PropertyNames.Description) { ni._descr = reader.ReadAsString(); } else if (propName == PropertyNames.Id) { ni._id = reader.ReadAsString(); } else if (propName == PropertyNames.InterfaceType) { ni._interfaceType = (NetworkInterfaceType)reader.ReadAsInt32().Value; } else if (propName == PropertyNames.Name) { ni._name = reader.ReadAsString(); } else if (propName == PropertyNames.PhysicalAddress) { ni._mac = reader.ReadAsBytes(); } } return(ni); }
public NetworkConnectionInfo(NetworkInterface ntwkInterface) { try { ntwkInfo = new Dictionary <string, IPAddress[]>(); if (ntwkInterface == null) { return; } this.nic = new NetworkInterfaceInternal(ntwkInterface); var ipProps = ntwkInterface.GetIPProperties(); foreach (var name in PropertyNames.GetIPAddressPropertyNames()) { ntwkInfo[name] = GetAddress(name, ipProps); } foreach (var addr in ipProps.UnicastAddresses) { var pre = addr.PrefixOrigin != NetInfo.PrefixOrigin.Manual; var suf = addr.SuffixOrigin != NetInfo.SuffixOrigin.Manual; _autoAssignedAddress = pre && suf; if (_autoAssignedAddress) { break; } } _dnsSuffix = ipProps.DnsSuffix; _initializedSuccessfully = true; } catch { _initializedSuccessfully = false; } }