如何通过C#获取网络打印机的MAC地址? [英] How can I get the MAC address of network printer via C#?
本文介绍了如何通过C#获取网络打印机的MAC地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取一个Intermec条码打印机的标识符,它使用网络接口,所以我想到了MAC地址.如何通过C#获取MAC地址?或者我可以直接获得打印机的序列号?
I wanna get the identifier of a Intermec barcode printer, it uses network interface, so I think of MAC address. How can I get the MAC address via C#? Or I can get the serial number of the printer directly?
推荐答案
我假设您具有网络打印机的IP地址,并且您的PC和打印机位于同一局域网中.您可以尝试一下该程序.
I am assuming that you have the IP address of the network printer and your pc and the printer are at the same local network. You can give this program a try.
static void Main(string[] args)
{
PhysicalAddress pa = LocateMacAddress(IPAddress.Parse("172.16.0.99"));
Console.WriteLine(pa.ToString());
Console.ReadKey();
}
static PhysicalAddress LocateMacAddress(IPAddress ipAddress)
{
if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
byte[] macAddressBytes = new byte[6];
int length = macAddressBytes.Length;
ArpErrorCodes c = (ArpErrorCodes)SendARP((uint)ipAddress.Address, 0, macAddressBytes, ref length);
if (c == ArpErrorCodes.None)
{
return new PhysicalAddress(macAddressBytes);
}
}
return PhysicalAddress.None;
}
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(uint DestIP, uint SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);
}
enum ArpErrorCodes
{
None = 0,
ERROR_GEN_FAILURE = 31,
ERROR_NOT_SUPPORTED = 50,
ERROR_BAD_NET_NAME = 67,
ERROR_BUFFER_OVERFLOW = 111,
ERROR_NOT_FOUND = 1168,
ERROR_INVALID_USER_BUFFER = 1784,
}
这篇关于如何通过C#获取网络打印机的MAC地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文