Formatting MAC address in C#

后端 未结 5 1205
灰色年华
灰色年华 2020-12-06 10:35

In my C# application, I want to get my MAC address by using NetworkInterface class as the following:

NetworkInterface nic in NetworkInterface.Ge         


        
5条回答
  •  有刺的猬
    2020-12-06 11:19

    Where you want to show that, you have to do this:

    txtMac.text=getFormatMac(GetMacAddress());
    public string GetMacAddress()
    
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        String sMacAddress = string.Empty;
        foreach (NetworkInterface adapter in nics)
        {
             if (sMacAddress == String.Empty)// solo retorna la mac de la primera tarjeta
             {
                  IPInterfaceProperties properties = adapter.GetIPProperties();
                  sMacAddress = adapter.GetPhysicalAddress().ToString();
              }
        }
        return sMacAddress;
    }
    
    public string getFormatMac(string sMacAddress)
    {
        string MACwithColons = "";
        for (int i = 0; i < macName.Length; i++)
        {
            MACwithColons = MACwithColons + macName.Substring(i, 2) + ":";
            i++;
        }
        MACwithColons = MACwithColons.Substring(0, MACwithColons.Length - 1);
    
        return MACwithColons;
    }
    

提交回复
热议问题