4

我发现此代码可以获取 MAC 地址,但它返回一个长字符串并且不包含“:”。

是否可以添加':'或拆分字符串并自己添加?

这是代码:

private object GetMACAddress()
{
    string macAddresses = "";

    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == OperationalStatus.Up)
        {
            macAddresses += nic.GetPhysicalAddress().ToString();
            break;
        }
    }

    return macAddresses;
 }

它返回 00E0EE00EE00 的值,而我希望它显示类似 00:E0:EE:00:EE:00 的内容。

有任何想法吗?

谢谢。

4

8 回答 8

11

我正在使用以下代码以您想要的格式访问 mac 地址:

public string GetSystemMACID()
        {
            string systemName = System.Windows.Forms.SystemInformation.ComputerName;
            try
            {
                ManagementScope theScope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");
                ObjectQuery theQuery = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter");
                ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);
                ManagementObjectCollection theCollectionOfResults = theSearcher.Get();

                foreach (ManagementObject theCurrentObject in theCollectionOfResults)
                {
                    if (theCurrentObject["MACAddress"] != null)
                    {
                        string macAdd = theCurrentObject["MACAddress"].ToString();
                        return macAdd.Replace(':', '-');
                    }
                }
            }
            catch (ManagementException e)
            {
                           }
            catch (System.UnauthorizedAccessException e)
            {

            }
            return string.Empty;
        }
于 2010-07-01T11:27:14.937 回答
9

您可以使用BitConverter.ToString()方法:

var hex = BitConverter.ToString( nic.GetPhysicalAddress().GetAddressBytes() );
hex.Replace( "-", ":" );
于 2010-07-01T11:30:42.997 回答
3

您可以使用此代码(使用 LINQ):

using System.Linq;
using System.Net;
using System.Net.NetworkInformation;

// ....

private static string GetMACAddress()
{
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == OperationalStatus.Up)
            return AddressBytesToString(nic.GetPhysicalAddress().GetAddressBytes());
    }

    return string.Empty;
}

private static string AddressBytesToString(byte[] addressBytes)
{
    return string.Join(":", (from b in addressBytes
                             select b.ToString("X2")).ToArray());
}
于 2010-07-01T11:35:38.293 回答
3

使用 LINQ 只需替换

macAddresses += nic.GetPhysicalAddress().ToString();
// Produces "00E0EE00EE00"

macAddresses += String.Join(":", nic.GetPhysicalAddress()
                                    .GetAddressBytes()
                                    .Select(b => b.ToString("X2"))
                                    .ToArray());
// Produces "00:E0:EE:00:EE:00"

您也可以使用ToString参数,例如,如果您喜欢00:e0:ee:00:ee:00更多,00:E0:EE:00:EE:00则可以传递"x2"而不是"X2".

于 2010-07-01T11:41:56.573 回答
1
   private string GetUserMacAddress()
        {
            var networkInterface = NetworkInterface.GetAllNetworkInterfaces()
                .FirstOrDefault(q => q.OperationalStatus == OperationalStatus.Up);

            if (networkInterface == null)
            {
                return string.Empty;
            }

            return BitConverter.ToString(networkInterface.GetPhysicalAddress().GetAddressBytes());
        }
于 2020-12-14T11:18:07.373 回答
0

使用GetAddressBytes方法:

    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("-");
        }
    }
于 2010-07-01T11:23:45.930 回答
0
function string GetSplitedMacAddress(string macAddresses)
{
    for (int Idx = 2; Idx <= 15; Idx += 3)
    {
        macAddresses = macAddresses.Insert(Idx, ":");
    }

    return macAddresses;
}
于 2010-07-01T11:23:48.173 回答
0

//MAC地址

var macAddress = NetworkInterface.GetAllNetworkInterfaces();
var getTarget = macAddress[0].GetPhysicalAddress();
于 2018-12-14T12:16:30.070 回答