How to get IPv4 using C# and not IPv6

deca300

Well-Known Member
Joined
Jan 28, 2010
Messages
138
Reaction score
0
Hi guys I was hoping to get some help here.

I wrote a small application to display the computers IP address and hostname.

The problem is, it returns the IPv6 the whole time(windows 7 PCs), only on windows XP machines it displays the IPv4.

What to I add to my code to display IPv4 only? Below is my code:

Any help or tips would be much appreciated :)


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;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Host Computer: " + Dns.GetHostName();
foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName()))
{
label2.Text = "IP Address: " + address;
}
}

private void label1_Click(object sender, EventArgs e)
{

}

private void label2_Click(object sender, EventArgs e)
{

}
}
}
 
Top
Sign up to the MyBroadband newsletter
X