示例#1
0
        public static void InternalSetupHelper(
            NetworkInterface[] nis,
            NetworkInterfaceType targetNetworkInterface,
            IPConfiguration ipConfiguration)
        {
            foreach (var networkInterface in nis)
            {
                // target only the working interface type
                if (networkInterface.NetworkInterfaceType == targetNetworkInterface)
                {
                    if (ipConfiguration != null)
                    {
                        Debug.WriteLine("Setting up static IP v4 configuration");

                        // setup static IPv4
                        networkInterface.EnableStaticIPv4(
                            ipConfiguration.IPAddress,
                            ipConfiguration.IPSubnetMask,
                            ipConfiguration.IPGatewayAddress);

                        // check if static DNS is to be configured
                        if (ipConfiguration.IPDns != null &&
                            ipConfiguration.IPDns.Length > 0)
                        {
                            networkInterface.EnableStaticIPv4Dns(ipConfiguration.IPDns);
                        }
                    }
                    else
                    {
                        if (networkInterface.IsDhcpEnabled)
                        {
                            networkInterface.EnableDhcp();
                        }
                        else
                        {
                            // setup static IPv4 with network config
                            networkInterface.EnableStaticIPv4(
                                networkInterface.IPv4Address,
                                networkInterface.IPv4SubnetMask,
                                networkInterface.IPv4GatewayAddress);

                            // check if static DNS is to be configured
                            if (networkInterface.IPv4DnsAddresses.Length > 0)
                            {
                                networkInterface.EnableStaticIPv4Dns(networkInterface.IPv4DnsAddresses);
                            }
                        }
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Checks if there is an IPAddress assigned to the specified network interface.
        /// </summary>
        public static bool CheckIP(NetworkInterfaceType interfaceType, IPConfiguration _ipConfiguration)
        {
            Debug.WriteLine($"Checking for IP in interface {interfaceType}");

            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface networkInterface in nis)
            {
                if (networkInterface.NetworkInterfaceType == interfaceType &&
                    networkInterface.IPv4Address[0] != '0')
                {
                    if (_ipConfiguration != null && _ipConfiguration.IPAddress != networkInterface.IPv4Address)
                    {
                        return(false);
                    }

                    Debug.WriteLine($"We have an IP: {networkInterface.IPv4Address}");

                    return(true);
                }
            }

            return(false);
        }