Can anyone help me with this little problem I have? I'm trying to get the IPv4 address of the network interface is being used to access the Internet, but I'm having issues with this. Here is my source code. I am using a network interface class method Get All network interfaces to obtain my information.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.Net.NetworkInformation;

namespace NetworkInformationMonitor
{
    public partial class Form1 : Form
    {
        private NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();

        private string NewLine = Environment.NewLine;

        private string Name;
        private string Speed;
        private string BytesSent;
        private string BytesReceived;
        private string OperationalStatus;
        private string PhysicalAddress;

        private int UpdateIndex = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Name = "Name: " + Interfaces[0].Description;
            OperationalStatus = "Operational Status: " + Interfaces[0].OperationalStatus.ToString();
            Speed = "Speed: " + Interfaces[0].Speed.ToString() + " bytes per second";
            BytesSent = "Bytes Sent: " + Interfaces[0].GetIPv4Statistics().BytesSent.ToString();
            BytesReceived = "Bytes Received: " + Interfaces[0].GetIPv4Statistics().BytesReceived.ToString();
            PhysicalAddress = "Physical Address: " + Interfaces[0].GetPhysicalAddress().ToString();

            UpdateIndex++;

            if (UpdateIndex == 30)
            {
                UpdateIndex = 0;
                richTextBox1.Text = Name + NewLine + PhysicalAddress + NewLine + OperationalStatus + NewLine + Speed + NewLine + BytesSent + NewLine + BytesReceived;
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.