public static IPConfig IPv4NetworkInterfaces(string nameInterface) { NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface Interface in Interfaces) { if (Interface.Name != nameInterface) { continue; } if (Interface.OperationalStatus != OperationalStatus.Up) { continue; } if (Interface.NetworkInterfaceType == NetworkInterfaceType.Loopback) { continue; } IPInterfaceProperties adapterProperties = Interface.GetIPProperties(); Console.WriteLine(Interface.Name); foreach (UnicastIPAddressInformation ip in Interface.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork) { continue; } _ipConfig = new IPConfig(); IPv4InterfaceProperties ipProperties = adapterProperties.GetIPv4Properties(); IPAddressCollection dnsServers = adapterProperties.DnsAddresses; var defaultGateway = adapterProperties.GatewayAddresses.Select(i => i?.Address).Where(x => x != null).FirstOrDefault(); _ipConfig.IsDhcpEnabled = ipProperties.IsDhcpEnabled; _ipConfig.IsDnsEnabled = DNSAutoOrStatic(Interface.Id); _ipConfig.IpAddress = ConvertHelper.CnvNullToString(ip.Address); _ipConfig.Subnet = ConvertHelper.CnvNullToString(ip.IPv4Mask); _ipConfig.Gateway = ConvertHelper.CnvNullToString(defaultGateway); if (dnsServers.Count > 0) { _ipConfig.DNS = ConvertHelper.CnvNullToString(dnsServers[0]); } _ipConfig.NICName = Interface.Description; _NICName = _ipConfig.NICName; } } return(_ipConfig); }
private void btnDefaul_Click(object sender, EventArgs e) { var ipConfig = new IPConfig() { IpAddress = "192.168.0.100", Subnet = "255.255.255.0", Gateway = "192.168.0.1", DNS = "8.8.8.8", IsDhcpEnabled = false, IsDnsEnabled = false }; NetworkManagement.setIP(ipConfig.IpAddress, ipConfig.Subnet, ipConfig.IsDhcpEnabled); NetworkManagement.setGateway(ipConfig.Gateway, ipConfig.IsDhcpEnabled); NetworkManagement.setDNS(NetworkManagement._NICName, ipConfig.DNS, ipConfig.IsDnsEnabled); txtIpAddress.Text = ipConfig.IpAddress; txtSubMask.Text = ipConfig.Subnet; txtGateway.Text = ipConfig.Gateway; txtDNS.Text = ipConfig.DNS; txtCurrentIp.Text = IP_DEFAULT; }