C# Get IP info of a specific Interface

Hello Experts,

I have a combo box where the user can select the Name of the available network interfaces. I want to then update labels with the IP, Subnet mask, gateway, DNS, etc of only that interface based on the Name. How can I do that?
triphenAsked:
Who is Participating?
 
it_saigeConnect With a Mentor DeveloperCommented:
This should get you started:

Form1.cs -
using System;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Windows.Forms;

namespace EE_Q28530673
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            comboBox1.DataSource = (from adapter in NetworkInterface.GetAllNetworkInterfaces()
                                    select new { DisplayMember = adapter.Description, ValueMember = adapter }).ToList();
            comboBox1.DisplayMember = "DisplayMember";
            comboBox1.ValueMember = "ValueMember";
        }

        private void OnSelectedValueChanged(object sender, EventArgs e)
        {
            if (sender is ComboBox)
            {
                var cmb = sender as ComboBox;
                var adapter = cmb.SelectedValue as NetworkInterface;
                if (adapter != null)
                {
                    var addresses = adapter.GetIPProperties().UnicastAddresses;
                    var gateways = adapter.GetIPProperties().GatewayAddresses;
                    textBox1.Text = $"Adapter: {adapter.Description}{Environment.NewLine}" +
                        $"IPv6 Address: {string.Join(", ", (from address in addresses where address.Address.AddressFamily == AddressFamily.InterNetworkV6 select address.Address))}{Environment.NewLine}" +
                        $"IPv4 Address: {string.Join(", ", (from address in addresses where address.Address.AddressFamily == AddressFamily.InterNetwork select address.Address))}{Environment.NewLine}" +
                        $"Subnet Mask: {string.Join(", ", (from address in addresses where address.Address.AddressFamily == AddressFamily.InterNetwork select address.IPv4Mask))}{Environment.NewLine}" +
                        $"Default Gateway: {string.Join(", ", (from gateway in gateways select gateway.Address))}{Environment.NewLine}" +
                        $"DNS: {string.Join(", ", (from address in adapter.GetIPProperties().DnsAddresses.Cast<IPAddress>() select address))}";
                }
            }
        }
    }
}

Open in new window


Form1.Designer.cs -
namespace EE_Q28530673
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(85, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Select Interface:";
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(103, 12);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(362, 21);
            this.comboBox1.TabIndex = 1;
            this.comboBox1.SelectedValueChanged += new System.EventHandler(this.OnSelectedValueChanged);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(15, 40);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(450, 209);
            this.textBox1.TabIndex = 2;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(477, 261);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.OnLoad);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox comboBox1;
        private System.Windows.Forms.TextBox textBox1;
    }
}

Open in new window


Produces the following output -Capture.PNGIf you drop down the combobox entries, you can choose another adapter to see it's configurations:Capture.PNGCapture.PNG
-saige-
1
 
Klavs RConnect With a Mentor Developer, owner of AlbusBitCommented:
See this example about getting all network interfaces and their IP address:
foreach(NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
   if(ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
   {
       Console.WriteLine(ni.Name);
       foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
       {
           if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
           {
               Console.WriteLine(ip.Address.ToString());
           }
       }
   }  
}

Open in new window

Source.
I hope this will help.
0
 
triphenAuthor Commented:
Exactly what I was looking for, thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.