可以在.NET Metro应用程序中获取网络MAC地址吗?

时间:2012-07-27 22:49:31

标签: networking windows-8 microsoft-metro windows-runtime

我需要在Metro UI应用中获取网络接口的MAC地址。据我所知,.NET 4.5 for Metro应用程序API根本不支持这一点。我错了吗?

3 个答案:

答案 0 :(得分:5)

您无法检索每个说法的MAC地址,但您可以检索硬件特定信息以识别计算机。

这是一篇完整的msdn文章,讨论主题:Guidance on using the App Specific Hardware ID (ASHWID) to implement per-device app logic (Windows)

小心只使用您需要的信息而不是完整的ID,因为它可能会根据您无用的信息(例如Dock Station字节)而改变。

以下是基于几个字节(CPU ID,内存大小,磁盘设备序列号和BIOS)的计算设备ID的代码示例:

string deviceSerial = string.Empty;
// http://msdn.microsoft.com/en-us/library/windows/apps/jj553431
Windows.System.Profile.HardwareToken hardwareToken = Windows.System.Profile.HardwareIdentification.GetPackageSpecificToken(null);
using (DataReader dataReader = DataReader.FromBuffer(hardwareToken.Id))
{
    int offset = 0;
    while (offset < hardwareToken.Id.Length)
    {
        byte[] hardwareEntry = new byte[4];
        dataReader.ReadBytes(hardwareEntry);

        // CPU ID of the processor || Size of the memory || Serial number of the disk device || BIOS
        if ((hardwareEntry[0] == 1 || hardwareEntry[0] == 2 || hardwareEntry[0] == 3 || hardwareEntry[0] == 9) && hardwareEntry[1] == 0)
        {
            if (!string.IsNullOrEmpty(deviceSerial))
            {
                deviceSerial += "|";
            }
            deviceSerial += string.Format("{0}.{1}", hardwareEntry[2], hardwareEntry[3]);
        }
        offset += 4;
    }
}

Debug.WriteLine("deviceSerial=" + deviceSerial);

答案 1 :(得分:0)

您无法从Metro风格应用程序访问低级别网络信息,因此使用股票SDK 。这是设计的。

答案 2 :(得分:-3)

    private void getDeviceInfos()
    {
        Profiles = Windows.Networking.Connectivity.NetworkInformation.GetConnectionProfiles();
        Adapter = Profiles[0].NetworkAdapter;

        Guid AdapterId = Adapter.NetworkAdapterId;
    }
    IReadOnlyList<Windows.Networking.Connectivity.ConnectionProfile> Profiles;
    Windows.Networking.Connectivity.NetworkAdapter Adapter;