获取MAC地址C#

获取MAC地址C#,c#,mac-address,C#,Mac Address,我发现这段代码用于获取MAC地址,但它返回一个长字符串,不包含“:” 是否可以添加“:”或拆分字符串并自己添加 代码如下: private object GetMACAddress() { string macAddresses = ""; foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { if (nic.OperationalStatus ==

我发现这段代码用于获取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这样的值

有什么想法吗

谢谢。

使用以下方法:

byte[]bytes=address.GetAddressBytes();
for(int i=0;i
函数字符串GetSplitedMacAddress(字符串MacAddress)
{

对于(int Idx=2;Idx我使用以下代码以您想要的格式访问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;
        }
您可以使用以下方法:

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


使用LINQ只是替换

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

您还可以使用
ToString
参数,例如,如果您喜欢
00:e0:ee:00:ee:00
多于
00:e0:ee:00:ee:00
,那么您只需传递
“x2”
而不是
“x2”

/MAC地址即可

var macAddress = NetworkInterface.GetAllNetworkInterfaces();
var getTarget = macAddress[0].GetPhysicalAddress();

地址在当前上下文中不存在?这是一个示例。显然,您必须将
地址
变量替换为您正在使用的变量。这是否提供了可靠的MAC地址?因为我遇到了获取虚拟地址的问题。至于我,当存在任何托管替代方案时,我总是试图避免WMI魔法。您可能需要一个。.Select()之后的ToArray()使其可编译。@安德鲁:谢谢,我最初使用.NET 4.0,其中
String.Join
具有重载接受
IEnumerable
参数。更新了我的代码以包含
.ToArray()
var hex = BitConverter.ToString( nic.GetPhysicalAddress().GetAddressBytes() );
hex.Replace( "-", ":" );
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());
}
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"
var macAddress = NetworkInterface.GetAllNetworkInterfaces();
var getTarget = macAddress[0].GetPhysicalAddress();
   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());
        }