C#获取本机mac地址
时间: 2024-06-12 16:02:38 浏览: 18
在C#中,获取本地计算机的MAC地址通常使用网络信息类(NetworkInformation)提供的方法。以下是一个简单的示例,展示了如何获取本地网卡的MAC地址:
```csharp
using System.Net.NetworkInformation;
public static string GetMacAddress()
{
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var nic in networkInterfaces)
{
if (nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
var macAddress = nic.GetPhysicalAddress().ToString("X2");
return macAddress;
}
}
return "无法获取MAC地址";
}
```
这段代码首先获取所有网络接口,然后检查哪些接口是活动的(非停用)且不是回环接口(Loopback),最后获取并返回这些接口的物理地址(MAC地址),格式为12进制的XX:XX:XX:XX:XX:XX。
相关问题
C# 获取windows当前ip地址与mac
可以使用以下代码获取Windows当前IP地址和MAC地址:
```csharp
using System.Net;
using System.Net.NetworkInformation;
using System.Linq;
public static string GetLocalIPAddress()
{
string ipAddress = "";
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ip.ToString();
break;
}
}
return ipAddress;
}
public static string GetMacAddress()
{
string macAddress = "";
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
if (adapter.OperationalStatus == OperationalStatus.Up && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
macAddress = string.Join(":", (from z in adapter.GetPhysicalAddress().GetAddressBytes() select z.ToString("X2")).ToArray());
break;
}
}
return macAddress;
}
```
上述代码中,GetLocalIPAddress()函数将返回Windows当前使用的IPv4地址,而GetMacAddress()函数将返回Windows当前使用的MAC地址。请注意,如果存在多个活动网络适配器,则GetMacAddress()函数返回的是第一个不是环回适配器的MAC地址。
软件获取mac的c#代码
在C#中获取MAC地址的代码可以使用System.Net.NetworkInformation命名空间中的NetworkInterface类来实现。下面是一个示例代码:
```csharp
using System;
using System.Net.NetworkInformation;
class Program
{
static void Main()
{
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in networkInterfaces)
{
if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
Console.WriteLine("MAC地址: " + networkInterface.GetPhysicalAddress().ToString());
}
}
}
}
```
这段代码通过调用`NetworkInterface.GetAllNetworkInterfaces()`方法获取所有网络接口,然后遍历每个网络接口,判断其类型是否为以太网或无线网卡,如果是,则调用`GetPhysicalAddress()`方法获取MAC地址。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)