2011-09-12 12 views
11

Nel mio C#, voglio avere il mio indirizzo MAC utilizzando NetworkInterface classe come il seguente:indirizzo formattazione MAC in C#

NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces() 
{ 
    mac = nic.GetPhysicalAddress() 
} 

Ma questo codice restituisce il MAC senza ':' o qualsiasi altro separatore.

Come posso recuperare il MAC in questo formato: 88:88:88:88:87:88 utilizzando il codice di cui sopra SOLO?

+8

"utilizzando il codice di cui sopra SOLO"? Se questo non restituisce l'indirizzo MAC con alcun separatore ... non puoi usarlo solo. – BoltClock

+0

Voglio formattarlo. hai un'idea di come? – gln

+0

@gln - check d risposta dove ho formattato l'indirizzo mac –

risposta

25

provare

mac = string.Join (":", (from z in nic.GetPhysicalAddress().GetAddressBytes() select z.ToString ("X2")).ToArray()); 
+7

Holy LINQ query e metodi di estensione, Batman! +1 – BoltClock

+5

in .NET 4 può essere anche più breve 'mac = string.Join (": ", nic.GetPhysicalAddress(). GetAddressBytes(). Select (b => b.ToString (" X2 ")));' – Firo

2

L'aiuto per il comando mostra un modo:

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx

PhysicalAddress address = adapter.GetPhysicalAddress(); 
    byte[] bytes = address.GetAddressBytes(); 
    for(int i = 0; i< bytes.Length; i++) 
    { 
     // Display the physical address in hexadecimal. 
     Console.Write("{0}", bytes[i].ToString("X2")); 
     // Insert a hyphen after each byte, unless we are at the end of the 
     // address. 
     if (i != bytes.Length -1) 
     { 
      Console.Write("-"); 
     } 
    } 
    Console.WriteLine(); 
+4

Questo può essere fatto anche più breve: 'BitConverter.ToString (byte [])' fa esattamente questa formattazione. – eFloh

+0

@eFloh: pubblica quella risposta. – BoltClock

+0

@BoltClock: questo è già nel post collegato sotto e si applica solo a questo post, in quanto non produce l'output richiesto. Vedere [questa risposta per ottenere l'indirizzo MAC C#] (http://stackoverflow.com/questions/3157246/getting-mac-address-c/3157309#3157309) per una risposta effettiva. – eFloh

2

Utilizzando la comment by eFloh per l'utilizzo di BitConverter ero in grado di fare quanto segue (supponendo mac è predefinito come una stringa).

foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) 
{ 
    mac = BitConverter.ToString(nic.GetPhysicalAddress().GetAddressBytes()).Replace('-', ':'); 

    //Do whatever else necessary with each mac... 
} 
0

provare qualcosa di simile:

// Insert Colons on MAC 
string MACwithColons = ""; 
for (int i = 0; i < MAC.Length; i++) { 
    MACwithColons = MACwithColons + MAC.Substring(i, 2) + ":"; 
    i++; 
} 
MACwithColons = MACwithColons.Substring(0, MACwithColons.Length - 1); // Remove the last colon 
1

Dove si vuole dimostrare che, si deve fare questo:

txtMac.text=getFormatMac(GetMacAddress()); 
public string GetMacAddress() 

{ 
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); 
    String sMacAddress = string.Empty; 
    foreach (NetworkInterface adapter in nics) 
    { 
     if (sMacAddress == String.Empty)// solo retorna la mac de la primera tarjeta 
     { 
       IPInterfaceProperties properties = adapter.GetIPProperties(); 
       sMacAddress = adapter.GetPhysicalAddress().ToString(); 
      } 
    } 
    return sMacAddress; 
} 

public string getFormatMac(string sMacAddress) 
{ 
    string MACwithColons = ""; 
    for (int i = 0; i < macName.Length; i++) 
    { 
     MACwithColons = MACwithColons + macName.Substring(i, 2) + ":"; 
     i++; 
    } 
    MACwithColons = MACwithColons.Substring(0, MACwithColons.Length - 1); 

    return MACwithColons; 
}