using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Diagnostics; using System.Net.NetworkInformation; using System.Net; namespace UTIL { #region Работа с сетевыми адаптерами public class SetNet { public void setIP(string caption, string ipaddress, Boolean dhcp) { // SetIP(EthStatic, "/c netsh interface ip set address \"" + EthName + "\" static " + Properties.Settings.Default.EthIPac + " " + Properties.Settings.Default.Subnet + " " + Properties.Settings.Default.EthDnsac + " & netsh interface ip set dns \"" + EthName + "\" static " + Properties.Settings.Default.EthDnsac); String arg = ""; if (!dhcp) { arg = $"/c netsh interface ipv4 set address \"{caption}\" static \"{ipaddress}\" \"255.0.0.0\""; // arg = $"/c netsh interface ipv4 set address \"{caption}\" static \"{ipaddress}\" \"255.0.0.0\" \"192.168.1.2\" & netsh interface ipv4 set dns \"{caption}\" static \"192.168.1.2\""; // arg = $"/c netsh interface ip set address \"{caption}\" static \"{ipaddress}\" \"255.0.0.0\" \"192.168.1.2\" & netsh interface ip set dns \"{caption}\" static \"192.168.1.2\""; } else { arg = $"/c netsh interface ipv4 set address \"{caption}\" dhcp & netsh interface ipv4 set dns \"{caption}\" dhcp"; // arg = $"/c netsh interface ip set address \"{caption}\" dhcp & netsh interface ip set dns \"{caption}\" dhcp"; } try { ProcessStartInfo psi = new ProcessStartInfo("cmd.exe"); psi.UseShellExecute = true; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.Verb = "runas"; psi.Arguments = arg; Process ps = Process.Start(psi); ps.WaitForExit(); if (ps.HasExited) { MessageBox.Show("Адрес изменен", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } public List getadapters() { List adapters = new List(); String ip = ""; String dns = ""; String nic = ""; String gw = ""; String dhcp = ""; String ipen = ""; String sub = ""; string[] NwDesc = { "TAP", "VMware", "Windows", "Virtual", "WAN", "Microsoft", "NDIS", "Bluetooth" }; // Adapter types (Description) to be ommited foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) { if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet && !NwDesc.Any(ni.Description.Contains)) // check for adapter type and its description { nic = ni.Name; Console.WriteLine($"Name: {nic} # Desc: {ni.Description} # Mac: {ni.GetPhysicalAddress().ToString()} # Status: {ni.OperationalStatus}"); if (ni.GetIPProperties().UnicastAddresses.Count == 0) ip = ""; else { foreach (UnicastIPAddressInformation ips in ni.GetIPProperties().UnicastAddresses) { if (ips.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { ip = ips.Address.ToString(); } } } if (ni.GetIPProperties().DnsAddresses.Count == 0) dns = ""; else { foreach (IPAddress dnsAdress in ni.GetIPProperties().DnsAddresses) { if (dnsAdress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { dns = dnsAdress.ToString(); } } } if (ni.GetIPProperties().GatewayAddresses.Count == 0) gw = ""; else { foreach (GatewayIPAddressInformation gwAdress in ni.GetIPProperties().GatewayAddresses) { gw = gwAdress.Address.ToString(); } } if (ni.GetIPProperties().DhcpServerAddresses.Count == 0) dhcp = ""; else { foreach (IPAddress dhcpAdress in ni.GetIPProperties().DhcpServerAddresses) { dhcp = dhcpAdress.ToString(); } } sub = "255.255.255.0"; if (ni.OperationalStatus.ToString() == "Down") ipen = "False"; else ipen = "True"; // Caption IPAddress IPSubnet DHCPEnabled IPEnabled Console.WriteLine($"{nic}-{ip}-{sub}-{dhcp}-{ipen}"); adapters.Add(new String[] { ni.Description, ip, sub, (dhcp == "" ? "False" : "True"), ipen, nic }); } } return adapters; } } #endregion }