Sunday, August 15, 2010

♦ Network Adapter name

Figure 1:Output Screen show Adapter name
The previous example (click see) Lab note we use c# find MAC Address only. this example will show how to get Lan card name or Adapter name

Syntax
       string NetworkInterfaces.Description

output
       Get the description of  the network interface.


Example :Get Network Adapter name
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace getmac
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            int num_nics=nics.Length;
            textBox1.Text += "Totals NIC(s) is " + num_nics + "  set(s)";
            textBox1.Text += Environment.NewLine;
            textBox1.Text += Environment.NewLine;

            for (int i=0; i< num_nics; i++)
            {
                PhysicalAddress mac = nics[i].GetPhysicalAddress();  
          
                textBox1.Text += i+") "+mac.ToString();
                textBox1.Text += "    ("+nics[i].Description+")";
                textBox1.Text += Environment.NewLine;
            }
        }
    }
}
Related titles
Get mac address

Saturday, August 14, 2010

♦ C# get mac address

Figure 1: Output Screen, MAC Address list at Local Machine

Our computer have a physical address 48 bytes size ()
00-12-3F-B1-17-A5           is Ethernet adapter Local Area Connection.
00-00-00-00-00-00-00-E0  is Tunnel adapter isatap (Microsoft ISATAP Adapter)
00-00-00-00-00-00-00-E0  is Tunnel adapter Local Area Connection* 9
                                          (Microsoft Teredo Tunneling Adapter)
if you want to know your PC mac address you can use command line
          c:\windows>ipconfig /all          [Enter]

you will found all your interface and your Network Interface Card (NIC) informations.

this program use  c# Class and method below.
      Namespace "System.Net.NetworkInformation"
      Class          "NetworkInterface"
      Method       "GetAllNetworkInterfaces()"
      Method       "GetPhysicalAddress()"

      Class           "PhysicalAddress"
      Method       "GetAddressBytes()"

Example Program:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace getmac
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
           NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            if (nics == null || nics.Length < 1)
            {
                textBox1.Text = "No network interfaces found.";
                return;
            }
        
            foreach (NetworkInterface adapter in nics)
            {
                PhysicalAddress address = adapter.GetPhysicalAddress();
                byte[] bytes = address.GetAddressBytes();

                for (int i = 0; i < bytes.Length; i++)
                {
                    textBox1.Text += bytes[i].ToString("X2"); // display in HEX

                    // Insert a hyphen after each byte, unless we are at the end of the 
                    if (i != bytes.Length - 1)
                    {
                        textBox1.Text += "-";
                    }
                }
                textBox1.Text += Environment.NewLine;
            }

         }
    }
}


if you did not understand "foreach loopread this where describe the easy your understand and use its.

Example Program: without foreach..loop

private void button1_Click(object sender, EventArgs e)
{
   NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
   int num_nics=nics.Length;
   for (int i=0; i< num_nics; i++)
   {
      PhysicalAddress mac = nics[i].GetPhysicalAddress();
      textBox1.Text += mac.ToString();
      textBox1.Text += Environment.NewLine;
   }
}

The result this program same with above but no-insert a hyphen after each byte.


and  http://msdn.microsoft.com/en-us/library/z1d0eff3.aspx for more useful method.

♦ foreach loop sample

Figure 1: Output Screen

This example show how to easy use "foreach loop" instead of  "for...loop".

Syntax:
          foreach (type identifier in expression) statement
                       ( 1       2         in     3         )
where:


type

           The type of identifier.

identifier

           The iteration variable that represents the collection element. If the iteration variable is a value type,
            it is effectively a read-only variable that cannot be modified.

expression

            Object collection or array expression. The type of the collection element must be convertible to the
             identifier type. Do not use an expression that evaluates to null.

             Evaluates to a type that implements IEnumerable or a type that declares a GetEnumerator method. In the latter case, GetEnumerator should either return a type that implements IEnumerator or declares all the methods defined in IEnumerator.

statement

            The embedded statement(s) to be executed.
more info with msdn.microsof.com

Here this is example program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            string [] fruits = {"apple","kiwi","pineapple","mango"};
            foreach (string listFruits in fruits)
            {
                textBox1.Text += listFruits.ToString()+Environment.NewLine;
            }
            textBox1.Text += Environment.NewLine;
            textBox1.Text += "Lenght is " + fruits.Length;
        }
    }
}

Friday, August 13, 2010

♦ Get a remote IP Addeess


Figure 1: Output Screen show find "youtube.com" ip address.


Figure 2: Output Screen find "Lan" ip address.



Figure 3: Output screen with invalid input.


This is example the way we can find Remote ip address.

Class Definition:
                  System.Net
                 - Dns class
                         - GetHostAddresses method

Syntax:
                    public static IPAddress[] GetHostAddresses (
                                        string hostNameOrAddress
                     )

Parameters : hostNameOrAddress
                      Type: System.String
                      The host name or IP address to resolve.

Return Value:
                    Type: System.Net.IPAddress[]
                    An array of type IPAddress that holds the IP addresses for the host that is specified by the
                    hostNameOrAddress parameter.
                   
                    The IPAddress class contains the address of a computer on an IP network.



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

        private void button1FindClick(object sender, EventArgs e)
        {
            string hostname = "";
            hostname = textBoxInput.Text;

                try
                {
                    IPAddress[] ipaddr = System.Net.Dns.GetHostAddresses(hostname);
                    for (int i = 0; i < ipaddr.Length; i++)
                    {
                        textBoxOutput.Text += "" + ipaddr[i].ToString() + Environment.NewLine;
                    }
                }
                catch (System.Net.Sockets.SocketException )
                {
                    textBoxOutput.Text = "No such host is known";
                }
          }

        private void button2ClearClick(object sender, EventArgs e)
        {
            textBoxOutput.Text = "";
        }
           
     }
    
 }

 
Remark : To protect some Error when you run this program in case you enter invalid format of hostname and/or IP Address format. so you should have ...
      try {
             ...
      }
      catch (System.Net.Sockets.SocketException) {
             ... your error message here ... 
      }

    If you have "run" this program without ...try exception... and input invalid hostname such as " 5a.5a 999" the debuger will report you with error message..."System.Net.Sockets.SocketException : no such host known". where become to me known what we do catch for...?    System.Net.Sockets.SocketException

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;
        }
    }
}

♫ Michael BublĂ© - Everything

♫ Michael bolton to love somebody