Domanda

Voglio ottenere l'indirizzo mac del dispositivo Bluetooth sul PC su cui è in esecuzione l'applicazione.

Ho provato quanto segue:

private void GetMacAddress()
{
     string macAddresses = "";
     foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
     {
          if (nic.OperationalStatus == OperationalStatus.Up)
          {
               macAddresses += nic.GetPhysicalAddress().ToString();
               Console.WriteLine(macAddresses);
          }
     }
}

Ma l'output non corrisponde a 'ipconfig / all' in commandprompt. Non stampa il mio indirizzo mac blueotths. Qualche soluzione?

Sono pronto per analizzare l'output che ottengo da 'ipconfig / all' ma come posso ottenere l'output come stringa?

È stato utile?

Soluzione

Puoi forse usare WMI per ottenere i risultati. Con la presente un collegamento a una soluzione WMI che attraversa i dispositivi di rete.

Sto pubblicando il codice qui nel caso in cui il sito Web non sia attivo, ma tutto il merito va all'autore originale, PsychoCoder. Usa WMI per ottenere l'indirizzo MAC in C #

E il codice:

//Namespace reference
using System.Management;

/// <summary>
/// Returns MAC Address from first Network Card in Computer
/// </summary>
/// <returns>MAC Address in string format</returns>
public string FindMACAddress()
{
    //create out management class object using the
    //Win32_NetworkAdapterConfiguration class to get the attributes
    //af the network adapter
    ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
    //create our ManagementObjectCollection to get the attributes with
    ManagementObjectCollection objCol = mgmt.GetInstances();
    string address = String.Empty;
    //My modification to the code
    var description = String.Empty;
    //loop through all the objects we find
    foreach (ManagementObject obj in objCol)
    {
        if (address == String.Empty)  // only return MAC Address from first card
        {
            //grab the value from the first network adapter we find
            //you can change the string to an array and get all
            //network adapters found as well
            if ((bool)obj["IPEnabled"] == true)
            {
                address = obj["MacAddress"].ToString();
                description = obj["Description"].ToString();
            }
        }
       //dispose of our object
       obj.Dispose();
    }
    //replace the ":" with an empty space, this could also
    //be removed if you wish
    address = address.Replace(":", "");
    //return the mac address
    return address;
}

Assicurati di includere il riferimento a System.Management. Per ottenere il nome del dispositivo di rete, è possibile utilizzare obj["Description"[.ToString();

Puoi anche dare un'occhiata a MSDN per quanto riguarda WMI, in particolare Win32_NetworkAdapterConfiguration Class

Spero che questo aiuti.

Altri suggerimenti

public static PhysicalAddress GetBTMacAddress()  {

    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) {

        // Only consider Bluetooth network interfaces
        if (nic.NetworkInterfaceType != NetworkInterfaceType.FastEthernetFx && 
            nic.NetworkInterfaceType != NetworkInterfaceType.Wireless80211){

            return nic.GetPhysicalAddress();
        }
    }
    return null;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top