Thursday 21 February 2013

Get Mac Address and Adapter Types ASP.NET C#


Get Mac Address and Adapter Types ASP.NET C#


Today I was thinking of getting the MAC address of a particular System.. I googled but didnt found anything suitable..So After Trying a little I was able to write my own custom Working Code which could also display the Adapter Types being Used...

First of all we have to create a custom Utility Class inside the namespace Util as follows:

Note: Here is a catch over here .. You have to manually reference to System.Management.dll within your project..

Go to Project> Right Click>Add Reference>Go To .Net Tab and Select System.Management.dll

Code for Utility.cs:


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Management.Instrumentation;
using System.Management;

namespace Util
{
     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;

        }
     }
}

MacAddress.aspx:


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Get Mac Address and Adapter Types</title>
</head>
<body>
    <form id="form1" runat="server">
    <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">

                    &nbsp;</td>

                <td>

                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

                </td>

            </tr>

        </table>
    </div>
    </form>
</body>
</html>

MacAddress.aspx.cs:


using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Util;

public partial class MacAddress : System.Web.UI.Page
{
    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);
    }
}




1 comment:

Thank You for Your Comments. We will get back to you soon.

back to top