Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Get MacAddress and Get All Adapter Types in ASP.NET

0.00/5 (No votes)
22 Aug 2012 1  
How to Get MacAddress and Get All Adapter Types in ASP.NETIn this demo we will see how to get MacAddress in ASP.NET in easy steps. In the first

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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">

                    &nbsp;</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);

        }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here