Esempio n. 1
0
        private void NetworkInterfaceComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (NetworkInterfaceComboBox.SelectedIndex != -1)
            {
                string nicNumber = NetworkInterfaceComboBox.SelectedValue.ToString();

                // Display the IP address
                if (_NetworkInterfaces != null)
                {
                    bool foundNic = false;
                    foreach (PcapNetworkInterface pni in _NetworkInterfaces)
                    {
                        if (nicNumber.Trim() == pni.NicNumber.Trim())
                        {
                            foundNic = true;
                            IpAddressLabel.Text = pni.IpAddress;
                            SelectedNicLabel.Text = pni.PcapDescription.Length > 20 ? pni.PcapDescription.Substring(0, 26) : pni.PcapDescription;
                            _SelectedNetworkInterface = pni;
                            break;
                        }
                    }
                    if (!foundNic)
                    {
                        IpAddressLabel.Text = "";
                    }
                }
                _CurrentClientNetworkInterface = _SelectedNetworkInterface.NicNumber;
            }
        }
Esempio n. 2
0
        private void NetworkInterfaceComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedNic = NetworkInterfaceComboBox.SelectedValue.ToString();

            foreach (PcapNetworkInterface pni in _NetworkInterfaces)
            {
                // Check to be sure we are using the correct pcap interface number
                if (selectedNic == pni.NicNumber)
                {
                    _SelectedNetworkInterface = pni;
                    break;
                }
            }
            _CurrentClientNetworkInterface = _SelectedNetworkInterface.NicNumber;
        }
        private void NetworkInterfaceComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            //_SelectedNetworkInterface = null;

            if (NetworkInterfaceComboBox.SelectedIndex != -1)
            {
                string nicNumber = NetworkInterfaceComboBox.SelectedValue.ToString();
                //SelectedNicLabel.Text = NetworkInterfaceComboBox.SelectedValue.ToString();
                //SelectedNicLabel.Text = nicNumber;
                // If we also wanted to get the displayed text we could use
                // the SelectedItem item property:
                // string s = ((USState)ListBox1.SelectedItem).LongName;

                // Display the IP address
                if (_NetworkInterfaces != null)
                {
                    bool foundNic = false;
                    foreach (PcapNetworkInterface pni in _NetworkInterfaces)
                    {
                        if (nicNumber.Trim() == pni.NicNumber.Trim())
                        {
                            foundNic = true;
                            IpAddressLabel.Text = pni.IpAddress;
                            //SelectedNicLabel.Text = pni.NicNumber;
                            SelectedNicLabel.Text = pni.PcapDescription;
                            _SelectedNetworkInterface = pni;
                            break;
                        }
                    }
                    if (!foundNic)
                    {
                        IpAddressLabel.Text = "";
                    }
                }
                _CurrentClientNetworkInterface = _SelectedNetworkInterface.NicNumber;
            }
        }
Esempio n. 4
0
        private void InitializeNetworkInterfacesComboBox()
        {
            //// Set the combobox so that the font for individual items can be changed
            //NetworkInterfaceComboBox.DrawMode = DrawMode.OwnerDrawFixed;

            //NetworkInterfaceComboBox.DrawItem += new DrawItemEventHandler(NetworkInterfaceComboBox_DrawItem);

            //BindingList<PcapNetworkInterface> _NetworkInterfaces = new BindingList<PcapNetworkInterface>();
            _NetworkInterfaces = new BindingList<PcapNetworkInterface>();

            string selectedNicNumber = string.Empty;

            // Get the Windows network interfaces
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            //foreach (NetworkInterface adapter in interfaces)
            //{
            //}

            IList<LivePacketDevice> networkPcapInterfaces = GetNetworkInterfaces();
            if (networkPcapInterfaces.Count > 0)
            {
                // Load up the list into the combobox
                foreach (LivePacketDevice lpd in networkPcapInterfaces)
                {
                    PcapNetworkInterface pni = new PcapNetworkInterface();
                    pni.NicNumber = networkPcapInterfaces.IndexOf(lpd).ToString();
                    //pni.NicName = lpd.Name;
                    if (lpd.Description != null)
                    {
                        string[] desc = lpd.Description.Split('\'');
                        pni.PcapDescription = desc[1];
                    }

                    // Need the guid and surrounding {}'s
                    string nicId = lpd.Name.Substring(20, lpd.Name.Length - 20);

                    // Find the NIC with the active IP address
                    foreach (NetworkInterface ni in interfaces)
                    {
                        if (ni.Id == nicId)
                        {
                            pni.NicDescription = ni.Name.Trim();
                            pni.Connected = ni.OperationalStatus == OperationalStatus.Up ? true : false;

                            // Get the IP address
                            if (pni.Connected && (pni.NicDescription.Contains("Local Area") || pni.NicDescription.Contains("Wireless")))
                            {
                                //pni.IpAddress = ni.GetIPProperties();
                                string hostName = string.Empty;
                                string ipAddress = string.Empty;
                                try
                                {
                                    hostName = Dns.GetHostName();
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("Warning - no HostName found: " + ex.Message, "Initialize Network Interfaces ComboBox - GetHostName", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }

                                if (hostName != string.Empty)
                                {
                                    try
                                    {
                                        ipAddress = GetMyIpAddress(hostName);
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show("Warning - no IP address found: " + ex.Message, "Initialize Network Interfaces ComboBox - GetMyIpAddress", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    }
                                }
                                pni.IpAddress = ipAddress == string.Empty ? "" : ipAddress;

                                if (ipAddress != string.Empty)
                                {
                                    _SelectedNetworkInterface = pni;
                                    selectedNicNumber = _SelectedNetworkInterface.NicNumber;
                                }
                            }
                            else
                            {
                                pni.IpAddress = "";
                            }
                            break;
                        }
                        //pni.Connected = ni.OperationalStatus == OperationalStatus.Up ? true : false;
                        //_NetworkInterfaces.Add(pni);
                        //break;
                    }
                    _NetworkInterfaces.Add(pni);
                }

                // Create a listing of NICs for the combobox

                BindingList<KeyValuePair<string, string>> nics = new BindingList<KeyValuePair<string, string>>();
                foreach (PcapNetworkInterface pi in _NetworkInterfaces)
                {
                    KeyValuePair<string, string> nic = new KeyValuePair<string, string>(pi.NicDescription, pi.NicNumber);
                    nics.Add(nic);
                }

                // Bind the SelectedValueChanged event to our handler for it.
                NetworkInterfaceComboBox.SelectedValueChanged += new EventHandler(NetworkInterfaceComboBox_SelectedValueChanged);

                NetworkInterfaceComboBox.DataSource = nics;

                // Display the NIC description
                NetworkInterfaceComboBox.DisplayMember = "Key";
                NetworkInterfaceComboBox.ValueMember = "Value";

                foreach (PcapNetworkInterface pni in _NetworkInterfaces)
                {
                    if (pni.NicNumber == selectedNicNumber)
                    {
                        // Reset the selected interface - it is getting reset to zero when the ValueMember gets assigned to "Value"
                        _SelectedNetworkInterface = pni;
                        break;
                    }
                }
                // Select the NIC with the IP address
                NetworkInterfaceComboBox.SelectedValue = _SelectedNetworkInterface.NicNumber;
            }
            else
            {
                NetworkInterfaceComboBox.Text = "No NICs Found";
                IpAddressLabel.Text = "No IP address found";
            }

            // Lock down the combobox control
            NetworkInterfaceComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            //NetworkInterfaceComboBox.BackColor = System.Drawing.SystemColors.ControlLightLight;

            // Ensure the form opens with no rows selected.
            //NetworkInterfaceComboBox.ClearSelected();

            NetworkInterfaceComboBox.Refresh();
        }
        private void NetworkInterfaceComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //_SelectedNetworkInterface = NetworkInterfaceComboBox.Items[NetworkInterfaceComboBox.SelectedIndex].ToString();
            string selectedNic = NetworkInterfaceComboBox.SelectedValue.ToString();

            foreach (PcapNetworkInterface pni in _NetworkInterfaces)
            {
                if (selectedNic == pni.NicNumber)
                {
                    _SelectedNetworkInterface = pni;
                    break;
                }
            }
        }