How to Get MacAddress and Get All Adapter Types in ASP.NET
In this demo we will see how to get MacAddress in ASP.NET in easy steps. In the first step we will write important part of code, we will add our code in Utility class, In the last step we will call some method has been created before to enable us to getting MacAddress for current user follow me.
In the Utiliy class will put this code
public class Utility
{
public static string GetMacAddress(string AdapterTypes)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapter where Name='" + AdapterTypes + "'");
ManagementObjectCollection moc = mos.Get();
string MACAddress = null;
if (moc.Count > 0)
{
foreach (ManagementObject mo in moc)
{
MACAddress = (string)mo["MACAddress"];
}
}
return MACAddress;
}
public static List<string> GetAllAdapterTypes()
{
List<string> AdapterTypes = new List<string>();
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapter Where AdapterType='Ethernet 802.3'");
foreach (ManagementObject mo in mos.Get())
{
AdapterTypes.Add(mo["Name"].ToString());
}
return AdapterTypes;
}
}
In Source Page will put this
<div>
<table class="style1">
<tr>
<td class="style2">
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</div>
In Code-Behind will put this
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
foreach (string item in Utility.GetAllAdapterTypes())
{ DropDownList1.Items.Add(item); }
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = Utility.GetMacAddress(DropDownList1.SelectedItem.Text);
}