예제 #1
0
        /// <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);
        }
예제 #2
0
        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);
        }
예제 #3
0
 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;
 }
예제 #4
0
 private void updateAllInterfaces()
 {
     foreach (InterfaceInformation inf in interfaces)
     {
         inf.CablePluggedIn -= inf_CablePluggedIn;
     }
     interfaces = Netsh.GetAllInterfaceInformation(interfaces);
     foreach (InterfaceInformation inf in interfaces)
     {
         inf.CablePluggedIn += inf_CablePluggedIn;
     }
     updateDataContext();
     InterfaceInformation.Serialize(interfaces);
 }
예제 #5
0
        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();
        }
예제 #6
0
        /// <summary>
        /// Gets a list of all information for the interfaces on a computer
        /// </summary>
        /// <returns>List of information for each interface</returns>
        internal static List <InterfaceInformation> GetAllInterfaceInformation(List <InterfaceInformation> currentInterfaces)
        {
            string interfaces = "";
            List <InterfaceInformation> interfaceList = new List <InterfaceInformation>();
            Process p = CreateNetShProcess("interface ipv4 show interfaces");

            p.Start();

            string output = "";

            while (!output.Contains("---"))
            {
                output = p.StandardOutput.ReadLine();
            }

            //next lines will be the interfaces

            while (!p.StandardOutput.EndOfStream)
            {
                string[] interfaceParts = p.StandardOutput.ReadLine().Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
                if (interfaceParts.Length >= 5)
                {
                    //likely a valid interface
                    string interfaceName = "";
                    for (int i = 4; i < interfaceParts.Length; i++)
                    {
                        interfaceName += interfaceParts[i] + " ";
                    }
                    interfaceName = interfaceName.Substring(0, interfaceName.Length - 1);
                    InterfaceInformation currentInterface = currentInterfaces.Find(ci => ci.Name.Equals(interfaceName));
                    interfaceList.Add(GetOrUpdateInterfaceInformation(interfaceName, currentInterface));
                    interfaces += interfaceName + ",";
                }
            }
            interfaces = interfaces.Substring(0, interfaces.Length - 1);

            return(interfaceList);
        }
예제 #7
0
        internal static void SetInterfaceState(List <InterfaceInformation> interfaces)
        {
            Process p = CreateIPConfigProcess();

            p.Start();

            string output = "";

            while (!p.StandardOutput.EndOfStream)
            {
                while (!output.Contains("adapter") && !p.StandardOutput.EndOfStream)
                {
                    output = p.StandardOutput.ReadLine();
                }
                string interfaceName = output.Substring(output.IndexOf("adapter") + 7);
                interfaceName = interfaceName.Substring(0, interfaceName.Length - 1).Trim(); //remove : at end
                InterfaceInformation interfaceInfo = interfaces.Find(inf => inf.Name.Equals(interfaceName) && inf.EnableAutoPlay);
                if (interfaceInfo != null)
                {
                    while (!output.Contains("Media State") && !(output.Contains("Connection-specific")))
                    {
                        output = p.StandardOutput.ReadLine();
                    }

                    if (output.Contains("Connection-specific"))
                    {
                        interfaceInfo.MediaConnected = true;
                    }
                    else
                    {
                        interfaceInfo.MediaConnected = false;
                    }
                }
                output = p.StandardOutput.ReadLine();
            }
        }
예제 #8
0
        /// <summary>
        /// Gets the current information for the interface
        /// </summary>
        /// <param name="interfaceName">Name of the interface to retrieve information for</param>
        /// <returns>Information for the interface</returns>
        internal static InterfaceInformation GetOrUpdateInterfaceInformation(string interfaceName, InterfaceInformation currentInterface)
        {
            InterfaceInformation information = new InterfaceInformation();

            if (currentInterface != null)
            {
                information = currentInterface;
            }

            Process p = CreateNetShProcess("interface ipv4 show addresses \"" + interfaceName + "\"");

            p.Start();

            string output = "";

            while (!output.Contains("Configuration for"))
            {
                output = p.StandardOutput.ReadLine();
            }

            //next line will be interface information


            information.Name = interfaceName;

            output = p.StandardOutput.ReadLine();

            //always confirm we're reading the correct lines
            if (output == null && p.StandardOutput.EndOfStream)
            {
                return(information);
            }
            if (output.Contains("DHCP"))
            {
                information.IsDHCP = output.Contains("Yes");
            }

            //next line IP
            output = p.StandardOutput.ReadLine();
            if (output == null && p.StandardOutput.EndOfStream)
            {
                return(information);
            }
            if (output.Contains("IP Address"))
            {
                string[] ipSplit = output.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);

                //split parts should be "IP", "Address", and the actual IP address

                information.IPAddress = ipSplit[2];
            }

            output = p.StandardOutput.ReadLine();
            if (output == null && p.StandardOutput.EndOfStream)
            {
                return(information);
            }
            //next line MASK
            if (output.Contains("Subnet Prefix"))
            {
                string[] maskSplit = output.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);

                information.IPMask = maskSplit[4].Substring(0, maskSplit[4].Length - 1); //remove trailing parenthesis
            }

            output = p.StandardOutput.ReadLine();
            if (output == null && p.StandardOutput.EndOfStream)
            {
                return(information);
            }
            //next line gateway
            if (output.Contains("Default Gateway"))
            {
                string[] gatewaySplit = output.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);

                information.Gateway = gatewaySplit[2];
            }

            return(information);
        }