コード例 #1
0
        private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            var loopbacks = new[] { NetworkInterfaceType.Loopback, NetworkInterfaceType.Tunnel };

            // look through all interfaces for the one whose address has changed
            foreach (var nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                // skip loopback & tunnel interfaces
                if (loopbacks.Contains(nic.NetworkInterfaceType))
                {
                    continue;
                }

                var ntwkInfo = new NetworkConnectionInfo(nic);

                if (nicState[nic.Id] == OperationalStatus.Up &&
                    nic.OperationalStatus == OperationalStatus.Down) // disconnect = Up -> Down
                {
                    OnNetworkDisconnect(new NetworkDisconnectEventArgs(nic));
                    break;
                }
                else if (nicState[nic.Id] != OperationalStatus.Up &&
                         nic.OperationalStatus == OperationalStatus.Up) // connect = any state -> Up
                {
                    OnNetworkConnect(new NetworkConnectEventArgs(nic));
                    break;
                }
                else if (!nicAddress[nic.Id].Equals(ntwkInfo.IPv4Address)) // changed address
                {
                    OnNetworkAddressChanged(new NetworkAddressChangedEventArgs(ntwkInfo));
                    break;
                }
            }
        }
コード例 #2
0
            static PropertyNames()
            {
                var n = new NetworkConnectionInfo(null);

                AutoAssignedIP   = GetPropertyName(() => n.AutoAssignedIPAddress);
                DefaultGateway   = GetPropertyName(() => n.DefaultGateway);
                DhcpServer       = GetPropertyName(() => n.DhcpServer);
                DnsServer        = GetPropertyName(() => n.DnsServer);
                DnsSuffix        = GetPropertyName(() => n.DnsSuffix);
                IPv4Address      = GetPropertyName(() => n.IPv4Address);
                IPv4Subnet       = GetPropertyName(() => n.IPv4SubnetMask);
                NetworkInterface = GetPropertyName(() => n.NetworkInterface);
            }
コード例 #3
0
            /// <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(JsonReader reader, Type objectType,
                                            object existingValue, JsonSerializer serializer)
            {
                var ni = new NetworkConnectionInfo(null);

                while (reader.Read())
                {
                    if (reader.TokenType == JsonToken.EndObject)
                    {
                        break;
                    }
                    if (reader.TokenType != JsonToken.PropertyName)
                    {
                        continue;
                    }

                    var propName = reader.Value.ToString(); // property name

                    if (PropertyNames.GetIPAddressPropertyNames().Contains(propName))
                    {
                        var values = reader.ReadAsString().Split(new[] { sep }, StringSplitOptions.RemoveEmptyEntries);
                        ni.ntwkInfo[propName] = values.Select(s => IPAddress.Parse(s)).ToArray();
                    }
                    else if (propName == PropertyNames.NetworkInterface)
                    {
                        // add a json converter for NetworkInterfaceInternal if none present
                        if (!serializer.Converters.Where(c => c.CanConvert(typeof(NetworkInterfaceInternal))).Any())
                        {
                            serializer.Converters.Add(new NetworkInterfaceInternal.JsonConverter());
                        }

                        reader.Read();
                        ni.nic = serializer.Deserialize <NetworkInterfaceInternal>(reader);
                    }
                    else if (propName == PropertyNames.AutoAssignedIP)
                    {
                        ni._autoAssignedAddress = bool.Parse(reader.ReadAsString());
                    }
                    else if (propName == PropertyNames.DnsSuffix)
                    {
                        ni._dnsSuffix = reader.ReadAsString();
                    }
                }

                return(ni);
            }
コード例 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NetworkAddressChangedEventArgs" /> class.
 /// </summary>
 /// <param name="networkInfo">The network information.</param>
 public NetworkAddressChangedEventArgs(NetworkConnectionInfo networkInfo)
 {
     _ni = networkInfo;
 }