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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Klavs RDeveloper, 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
it_saigeDeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
triphenAuthor Commented:
Exactly what I was looking for, thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.