-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNETUtils.cs
More file actions
52 lines (45 loc) · 1.61 KB
/
NETUtils.cs
File metadata and controls
52 lines (45 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
namespace VS2010Commons
{
public class NETUtils
{
/// <summary>
/// returns the mac address of the NIC with max speed.
/// </summary>
/// <returns></returns>
public static string GetMacAddress()
{
const int MIN_MAC_ADDR_LENGTH = 12;
string macAddress = "";
long maxSpeed = -1;
StringBuilder sb = new StringBuilder();
HashSet<string> macs = new HashSet<string>();
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
// log.Debug("Found MAC Address: " + nic.GetPhysicalAddress().ToString() + " Type: " + nic.NetworkInterfaceType);
string tempMac = nic.GetPhysicalAddress().ToString();
if (nic.Speed > maxSpeed && !String.IsNullOrEmpty(tempMac) && tempMac.Length >= MIN_MAC_ADDR_LENGTH)
{
// log.Debug("New Max Speed = " + nic.Speed + ", MAC: " + tempMac);
maxSpeed = nic.Speed;
macAddress = tempMac;
}
if (!String.IsNullOrEmpty(tempMac) && tempMac.Length >= MIN_MAC_ADDR_LENGTH)
{
macs.Add(tempMac);
}
}
foreach (String s in macs)
{
if (sb.Length > 0)
sb.Append(";");
sb.Append(s);
}
return sb.ToString();
}
}
}