Sunday, August 8, 2010

♦ Get hostname and IP address

Figure 1: The result screen
"C sharp .Net" have rich and easy command for get hostname and IP address.
   ....
   using System.Net;
   ...
  myIPv4 = System.Net.Dns.GetHostEntry(thishostname).AddressList[2].ToString();

  Namespace : System.Net
  Class          : Dns
  Method  GetHostEntry
                Dns.GetHostEntry (IPAddress), Resolves an IP address to an IPHostEntry instance.
                Dns.GetHostEntry (String), Resolves a host name or IP address to an IPHostEntry instance.

                public IPAddress[]  = hostinfo.AddressList[index]

Here is a part of program Form1.cs

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

        private void btnRUN_Click(object sender, EventArgs e)
        {
            
            string thishostname = Dns.GetHostName();
            string IPv4,IPv6,IPtunnel="";

            IPHostEntry hostinfo = Dns.GetHostEntry(thishostname);
            string hostname = hostinfo.HostName;

            textBox1hostname.Text = thishostname;
            for (int index = 0; index < hostinfo.AddressList.Length; index++)
            {
                if (index == 0)
                {
                    IPv6 = hostinfo.AddressList[index].ToString();
                    textBoxIPv6.Text = IPv6;
                }
                if (index == 1)
                {
                    IPtunnel = hostinfo.AddressList[index].ToString();
                    textBoxTunnelAdaptor.Text = IPtunnel;
                }
                if (index ==2)
                {
                    IPv4 = hostinfo.AddressList[index].ToString();
                    textBoxIPv4.Text = IPv4;
                }
               
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBoxMultiLine_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

*-----------------------------------------------------------------------------*

Another Way


Figure 2: output of gethostname3.cs

Also C# have serveral class where help you to get the hostname. this program below need "System.Net.NetworkInformation" and "System.Net"
      - Class IPGlobalProperties
      - Public Properties  "HostName" Gets the host name for the local computer.
              - IPGlobalProperties.HostName


--------------------------------------------------------------------------------


Program Name: gethostname3.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;

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

        private void button1_Click(object sender, EventArgs e)
        {
            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
            textBox1.Text += computerProperties.HostName;
        }
    }
}

No comments:

Post a Comment