SharpDevelop C# Show IP Address Windows form

deca300

Well-Known Member
Joined
Jan 28, 2010
Messages
138
Reaction score
0
Hi guys

I'm sure someone can help me, Im trying to just write this very basic app.

One function, user open the .exe and it displays the local IP and host name.

What I have used:

Please click to view the form,

form.JPG

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 IPChecker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Host Computer: " + Dns.GetHostName();
foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
{
label2.Text = "IP Adress: " + adress;
}
}
}
}


What did I miss out? Or what do I need for this?
 
Just tested your code out and it worked fine on my pc. I think just double clicked on the two labels to add them into the code and test it out.

Here is what i added in
Code:
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

Full code
Code:
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 adress in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                label2.Text = "IP Adress: " + adress;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
 
It's "address", but close enough :p
And yeah, as above, your code's fine.
 
Hi guys another question :)

some people have some virtual network adapters on their PC's... Now once they run this app for always shows the VM IP address and not the Local LAN... How can I get around that ? Any ideas ?

Appreciate the help!
 
Hi guys another question :)

some people have some virtual network adapters on their PC's... Now once they run this app for always shows the VM IP address and not the Local LAN... How can I get around that ? Any ideas ?

Appreciate the help!

Open a UDP connection to a machine/server on the network and use the IP address from that connection? I don't know the code out of my head but if this is what you are looking for it will look something like this:

var client = new UdpClient(serverName, portNumber);
var ip = client.GetIPAddress();

...or something like that.


That ip will be the one your machine uses on that NIC for that network.

EDIT:

http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.localendpoint.aspx

var client = new UdpClient();
client.Connect(server, 1);
var ip = IPAddress.Parse ((IPEndPoint)client.Client.LocalEndPoint).Address.ToString();

...or something like that :p (typing on iPad so probably not 100%)
 
Last edited:
hey man, looks good! I'm going to read up on this a bit thanks a lot! :D
 
Hi guys another question :)

some people have some virtual network adapters on their PC's... Now once they run this app for always shows the VM IP address and not the Local LAN... How can I get around that ? Any ideas ?

Appreciate the help!

Your bit of code:
Code:
        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "Host Computer: " + Dns.GetHostName();
            foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                label2.Text = "IP Adress: " + adress;
            }
        }

clears out the LAN IP address and just shows the VLAN address because that's the last one in your foreach loop. It shoudl be along the lines of:

Code:
        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "Host Computer: " + Dns.GetHostName();
            label2.Text = "IP Adresses: ";
            foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                label2.Text = label2.Text +" " + adress;
            }
        }
 
Hi guys,

I know its been a while. I have another question, what do I need to add to show the domain?

Please help :)
 
Top
Sign up to the MyBroadband newsletter
X