In this article I am going to explain how to get MAC address of
system using asp.net.
In previous article I have explained how to select recordsafter a specific record/Id in sql server, how to generate limited random numberand add hyphen after each 4th character in asp.net and how to check string isPalindrome or not using Reverse function or without reverse function in sqlserver.
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.
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