【问题标题】:How can I get the MAC address of network printer via C#?如何通过 C# 获取网络打印机的 MAC 地址?
【发布时间】:2013-10-22 05:45:19
【问题描述】:

我想获取 Intermec 条码打印机的标识符,它使用网络接口,所以我想到了 MAC 地址。如何通过 C# 获取 MAC 地址?或者我可以直接得到打印机的序列号?

【问题讨论】:

    标签: c# networking mac-address


    【解决方案1】:

    我假设您拥有网络打印机的 IP 地址,并且您的电脑和打印机位于同一个本地网络。你可以试试这个程序。

        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,
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      • 1970-01-01
      相关资源
      最近更新 更多