Sunday, May 21, 2017

Asp.net : Get MAC address of system

In this article I am going to explain how to get MAC address of system using asp.net.


Implementation:
MAC (Media access control) address is the unique identifier of each device. To know MAC address go to command prompt and type ipconfig/all, hit the enter button.

Asp.net : Get MAC address of system


 HTML Markup:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnsubmit" runat="server" Text="Get MAC Address"/>

        <br />
        <asp:Label ID="lblname" runat="server" Text="Label" Visible="false"></asp:Label>
    </div>
    </form>
</body>
</html>

Add namespace
C# Code:
using System.Net.NetworkInformation;

VB.net Code:
Imports System.Net.NetworkInformation

On button click write the below given code:

C# Code:
string amacaddress = "";

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        try
        {
            NetworkInterface[] anics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in anics)
            {
                if (amacaddress == String.Empty)
                {
                    IPInterfaceProperties properties = adapter.GetIPProperties();
                    amacaddress = adapter.GetPhysicalAddress().ToString();
                    lblname.Visible = true;
                    lblname.Text = "MAC Address is :- " + amacaddress;
                }
            }
        }
        catch (Exception ex) { }      
    }

VB.net Code:
    Dim amacaddress As String
    Protected Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
        Try
            Dim anics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
            For Each adapter As NetworkInterface In anics
                If amacaddress = [String].Empty Then
                    Dim properties As IPInterfaceProperties = adapter.GetIPProperties()
                    amacaddress = adapter.GetPhysicalAddress().ToString()
                    lblname.Visible = True
                    lblname.Text = "MAC Address is :- " + amacaddress
                End If
            Next
        Catch ex As Exception
        End Try
    End Sub
End Class


No comments:

Post a Comment