private void buttonApply_Click(object sender, EventArgs e)
{
if (!ValidateConfigs())
return;
// validation passed; starting configuring
Hide();
GadgetForm.AutoRefreshAllowed = false;
splash = LoadingForm.Create("Configuring interface \"" + nic.Name + "\" ...");
// XP sets gateway metric along with interface metric, if gwmetric is auto, so we do this first
if (interfaceMetric.Text != nic.InterfaceMetric.ToString())
nic.SetInterfaceMetric(interfaceMetric.Text);
if (nic.IPv4Enabled)
{
if (dhcpIPEnabled.Checked || ipv4Address.Count == 0)
nic.SetDhcp(1 + Convert.ToInt32(dhcpDnsEnabled.Checked || ipv4Dns.Count == 0));
else
{
if (nic.Dhcpv4Enabled > 0)
nic.SetIPv4Address(ipv4Address[0].Address, ipv4Address[0].Subnet);
if (ipv4Address.Count > 0)
{
foreach (NetworkInterface.IPHostAddress ip in ipv4Address)
{
if (nic.IPv4Address.Find((i) => i.Address == ip.Address) != null &&
nic.IPv4Address.Find((i) => i.Address == ip.Address).Subnet == ip.Subnet &&
nic.Dhcpv4Enabled == 0)
continue;
if (nic.IPv4Address.Find((i) => i.Address == ip.Address) != null ||
nic.Dhcpv4Enabled > 0)
nic.DeleteIPv4Address(ip.Address, ip.Subnet);
nic.AddIPv4Address(ip.Address, ip.Subnet);
}
foreach (NetworkInterface.IPHostAddress ip in nic.IPv4Address)
if (ipv4Address.Find((i) => i.Address == ip.Address) == null)
nic.DeleteIPv4Address(ip.Address, ip.Subnet);
foreach (NetworkInterface.IPGatewayAddress ip in ipv4Gateway)
{
if (nic.IPv4Gateway.Find((i) => i.Address == ip.Address) != null &&
nic.IPv4Gateway.Find((i) => i.Address == ip.Address).GatewayMetric == ip.GatewayMetric &&
nic.Dhcpv4Enabled == 0)
continue;
if (nic.IPv4Gateway.Find((i) => i.Address == ip.Address) != null ||
nic.Dhcpv4Enabled > 0)
nic.DeleteIPv4Gateway(ip.Address);
nic.AddIPv4Gateway(ip.Address, ip.GatewayMetric.ToString());
}
foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv4Gateway)
if (ipv4Gateway.Find((i) => i.Address == ip.Address) == null)
nic.DeleteIPv4Gateway(ip.Address);
}
}
foreach (string dns in ipv4Dns)
{
if (nic.IPv4DnsServer.Contains(dns) && nic.Dhcpv4Enabled < NetworkInterface.Dhcp.IPnDns)
continue;
nic.AddIPv4DnsServer(dns);
}
foreach (string dns in nic.IPv4DnsServer)
if (!ipv4Dns.Contains(dns))
nic.DeleteIPv4DnsServer(dns);
if (netbiosEnabled.Checked == false && nic.NetbiosEnabled != NetworkInterface.Netbios.Disabled)
nic.SetNetBios(false);
if (netbiosEnabled.Checked && nic.NetbiosEnabled == NetworkInterface.Netbios.Disabled)
nic.SetNetBios(true);
if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1)
if (ipv4Mtu.Text != nic.IPv4Mtu)
nic.SetIPv4Mtu(ipv4Mtu.Text);
}
if (nic.IPv6Enabled)
{
foreach (NetworkInterface.IPHostAddress ip in ipv6Address)
{
if (nic.IPv6Address.Contains(ip.Address, ip.Subnet))
continue;
nic.AddIPv6Address(ip.Address, ip.Subnet);
}
foreach (NetworkInterface.IPHostAddress ip in nic.IPv6Address.All)
if (ipv6Address.Find((i) => i.Address == ip.Address) == null)
nic.DeleteIPv6Address(ip.Address);
foreach (NetworkInterface.IPGatewayAddress ip in ipv6Gateway)
{
if (nic.IPv6Gateway.Contains(new NetworkInterface.IPGatewayAddress(ip.Address, ip.GatewayMetric)))
continue;
nic.AddIPv6Gateway(ip.Address, ip.GatewayMetric.ToString());
}
foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv6Gateway)
if (ipv6Gateway.Find((i) => i.Address == ip.Address) == null)
nic.DeleteIPv6Gateway(ip.Address);
foreach (string ip in ipv6Dns)
{
if (nic.IPv6DnsServer.Contains(ip))
continue;
nic.AddIPv6DnsServer(ip);
}
foreach (string ip in nic.IPv6DnsServer)
if (!ipv6Dns.Contains(ip))
nic.DeleteIPv6DnsServer(ip);
if (routerDiscoveryEnabled.Checked != nic.IPv6RouterDiscoveryEnabled)
nic.SetRouterDiscovery(routerDiscoveryEnabled.Checked);
if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1)
if (ipv6Mtu.Text != nic.IPv6Mtu)
nic.SetIPv6Mtu(ipv6Mtu.Text);
}
splash.UpdateStatus("Waiting for changes to be applied ...");
timer.AutoReset = false;
timer.Interval = 10000;
timer.Elapsed += timer_Elapsed;
timer.Start();
System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
}