/// <summary> /// Sets the interface given the specified information. /// </summary> /// <param name="savedInterface">interface information to be set</param> /// <returns>Returns true if successful</returns> internal static bool SetInterface(SavedInterface savedInterface) { if (savedInterface.IsDHCP) { string arguments = string.Format("interface ipv4 set address name=\"{0}\" source=dhcp", savedInterface.Name); Process p = CreateNetShProcess(arguments); p.Start(); //need to wait for results to update in netsh. InterfaceInformation newInformation = GetOrUpdateInterfaceInformation(savedInterface.Name, null); int maxRetries = 60; int retries = 0; while (!newInformation.IsDHCP || newInformation.IPAddress == null) { //retry until the result works, or we've hit a max number of retries. System.Threading.Thread.Sleep(100); newInformation = GetOrUpdateInterfaceInformation(savedInterface.Name, null); retries++; if (retries >= maxRetries) { return(false); } } if (newInformation.IsDHCP) { return(true); } } else { string arguments = string.Format("interface ipv4 set address name=\"{0}\" sour=static address={1} mask={2} gateway={3}", savedInterface.Name, savedInterface.IPAddress, savedInterface.IPMask, savedInterface.Gateway); Process p = CreateNetShProcess(arguments); p.Start(); //need to wait for results to update in netsh. //need to confirm the change worked InterfaceInformation newInformation = GetOrUpdateInterfaceInformation(savedInterface.Name, null); int maxRetries = 60; int retries = 0; while (newInformation.IPAddress == null || !newInformation.IPAddress.Equals(savedInterface.IPAddress)) { System.Threading.Thread.Sleep(100); newInformation = GetOrUpdateInterfaceInformation(savedInterface.Name, null); retries++; if (retries >= maxRetries) { Console.WriteLine("Max retries hit"); return(false); } } if (!newInformation.IsDHCP || newInformation.IPAddress.Equals(savedInterface.IPAddress)) { return(true); } } return(false); }
private void btnRemoveSavedInterface_Click(object sender, RoutedEventArgs e) { if (listSavedInterfaces.SelectedValue != null) { savedInterfaces.Remove((SavedInterface)listSavedInterfaces.SelectedValue); SavedInterface.Serialize(savedInterfaces); updateSavedInterfaces(); } }
public static SavedInterface ConvertInterfacetoSavedInterface(InterfaceInformation intf, string savedName) { SavedInterface newInt = new SavedInterface(); newInt.SavedInterfaceName = savedName; newInt.Name = intf.Name; newInt.IPAddress = intf.IPAddress; newInt.IPMask = intf.IPMask; newInt.Gateway = intf.Gateway; newInt.IsDHCP = intf.IsDHCP; return(newInt); }
public NetworkAutoplay(InterfaceInformation interfaceInfo) { savedInterfaces = SavedInterface.Deserialize(); inf = interfaceInfo; InitializeComponent(); lblNetworkAdapterName.Content = inf.Name; lblCurrentIPAddress.Content = inf.IPAddress; fillInSavedInterfaceList(); this.Topmost = true; this.Show(); this.MouseDown += NetworkAutoplay_MouseDown; }
private void btnAddSavedInterface_Click(object sender, RoutedEventArgs e) { if (listInterfaces.SelectedValue != null) { wndAddInterface addInterface = new wndAddInterface(listInterfaces.SelectedValue.ToString()); if (addInterface.ShowDialog() == true) { savedInterfaces.Add(addInterface.SavedInterface); SavedInterface.Serialize(savedInterfaces); updateSavedInterfaces(); } } }
private void btnSave_Click(object sender, RoutedEventArgs e) { SavedInterface newInterface = new SavedInterface(); newInterface.Name = txtInterfaceName.Text; newInterface.SavedInterfaceName = txtSavedName.Text; newInterface.IPAddress = txtIPAddress.Text; newInterface.IPMask = txtMask.Text; newInterface.Gateway = txtGateway.Text; newInterface.IsDHCP = ((ComboBoxItem)cbDHCP.SelectedValue).Content.Equals("Yes"); SavedInterface = newInterface; this.DialogResult = true; this.Close(); }
private void btnSetDefaultInterface_Click(object sender, RoutedEventArgs e) { SavedInterface newInt = SavedInterface.ConvertInterfacetoSavedInterface((InterfaceInformation)listInterfaces.SelectedValue, "Default"); //if a previous default interface existed, remove it. SavedInterface defaultInterface = savedInterfaces.Find(si => si.Name.Equals(((InterfaceInformation)listInterfaces.SelectedValue).Name) && si.SavedInterfaceName.Equals("Default")); if (defaultInterface != null) { savedInterfaces.Remove(defaultInterface); } savedInterfaces.Add(newInt); SavedInterface.Serialize(savedInterfaces); updateSavedInterfaces(); }
public MainWindow() { InitializeComponent(); interfaces = InterfaceInformation.Deserialize(); savedInterfaces = SavedInterface.Deserialize(); updateAllInterfaces(); listInterfaces.ItemsSource = interfaces; networkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(); System.Timers.Timer t = new System.Timers.Timer(); t.Interval = 1000; t.Elapsed += delegate { IPConfig.SetInterfaceState(interfaces); }; t.Start(); }