Click here to Skip to main content
15,886,716 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL3 min read 5.5K   2  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --