How to Get MAC address programatically in c# for a windows mobile 6.0 device

纵饮孤独 提交于 2019-11-30 04:00:33

问题


How to Get MAC address programatically in c# for a windows mobile 6.0 device? The System.Net.NetworkInformation is not supported in ,net compatc framework 3.5.


回答1:


I know it's been awhile, but I needed this and found I could use OpenNETCF version of the code above with a tweak:

INetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
//for each j you can get the MAC 
PhysicalAddress address = nics[0].GetPhysicalAddress();
byte[] bytes = address.GetAddressBytes();
for(int i = 0; i < bytes.Length; i++) {
    // Display the physical address in hexadecimal. 
    Console.Write("{0}", bytes[i].ToString("X2"));
    // Insert a hyphen after each byte, unless we are at the end of the address. 
    if(i != bytes.Length - 1) {
        Console.Write("-");
    }
}



回答2:


We can use MDSDK in this type of problems like

using PsionTeklogix.Peripherals;
namespace EnumAdapters
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ArrayList pList = null;
            string[] lStatistic = null;

            try
            {
                pList = Peripherals.EnumerateAdapters();
            }
            catch (Exception ex)
            {
                MessageBox.Show("failed with\r\n" + ex.Message, "EnumerateAdapters()");
                Close();
                return;
            }

            listBox1.Items.Clear();

            foreach (string AdapterName in pList)
            {
                try
                {
                    listBox1.Items.Add(AdapterName + (Peripherals.IsAdapterPresent(AdapterName) ? " is present" : " is NOT present") + (Peripherals.IsWirelessAdapter(AdapterName) ? " [wireless adapter] " : ""));
                    lStatistic = Peripherals.GetAdapterStatistics(AdapterName); // See Note 1
                    foreach (string StatInfo in lStatistic)
                    {
                        if (StatInfo.StartsWith("Local MAC Address"))
                        {
                            listBox1.Items.Add("» " + StatInfo);
                            break;
                         }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Close();
                    return;
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/4143323/how-to-get-mac-address-programatically-in-c-sharp-for-a-windows-mobile-6-0-devic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!