Get a list of all network interfaces

To use the following C# code you have to add a reference to SharpPcap library in your project.

class Computer
{
    public static List<NetworkInterface> NetworkInterfaces
    {
	get
	{
    	    try
	    {
		List<NetworkInterface> list = new List<NetworkInterface>();
		foreach (WinPcapDevice dev in WinPcapDeviceList.Instance)
		{
			NetworkInterface ni = new NetworkInterface();
			ni.Adapter = dev.Name.Replace("rpcap://", "");
			ni.Description = dev.Description;
		        ni.Name = dev.Interface.FriendlyName;
			ni.Gateway = dev.Interface.GatewayAddress;
			ni.MacAddress = dev.Interface.MacAddress;
 
			foreach (PcapAddress addrress in dev.Addresses)
			{
				if (addrress.Addr.ipAddress != null)
				{
	 			    if (addrress.Addr.ipAddress.AddressFamily 
                                           == AddressFamily.InterNetwork)			
                                    {
					ni.IPAddress = addrress.Addr.ipAddress;
					ni.SubnetMask = addrress.Netmask.ipAddress;
				    }
				}
			}
			list.Add(ni);
		}
		return list;
	    }
	    catch
	    {
		return null;
	    }
        }
    }
}

NetworkInterface class

class NetworkInterface
{
        private String _adapter;
	private String _name;
	private IPAddress _ipaddress;
	private IPAddress _subnetmask;
	private IPAddress _gateway;
	private PhysicalAddress _macaddress;
	private String _description;
 
	/// <summary>
	/// default constructor
	/// </summary>
	public NetworkInterface()
	{
	        _adapter = "";
		_name = "";
		_ipaddress = null;
		_subnetmask = null;
		_gateway = null;
		_macaddress = null;
		_description = "";
	}
 
	/// <summary>
	/// get or set adapter friendly name
	/// </summary>
	public String Name
	{
		get
		{
		        return _name;
		}
 
		set
		{
			_name = value;
		}
	}
 
	/// <summary>
	/// get or set adapter name
	/// </summary>
	public String Adapter
	{
		get
		{
			return _adapter;
		}
 
		set
		{
			_adapter = value;
		}
	}
 
	/// <summary>
	/// get or set adapter ip address
	/// </summary>
	public IPAddress IPAddress
	{
		get
		{
			return _ipaddress;
		}
 
		set
		{
			try
			{
				_ipaddress = value;
			}
			catch
			{
				_ipaddress = null;
			}
		}
	}
 
	/// <summary>
	/// get or set adapter subnet mask
	/// </summary>
	public IPAddress SubnetMask
	{
		get
		{
			return _subnetmask;
		}
 
		set
		{
			try
			{
				_subnetmask = value;
			}
			catch
			{
				_subnetmask = null;
			}
		}
	}
 
	/// <summary>
	/// get or set adapter gateway ip address
	/// </summary>
	public IPAddress Gateway
	{
		get
		{
			return _gateway;
		}
 
		set
		{
			try
			{
				_gateway = value;
			}
			catch
			{
				_gateway = null;
			}
		}
	}
 
	/// <summary>
	/// get or set adapter mac address
	/// </summary>
	public PhysicalAddress MacAddress
	{
		get
		{
			return _macaddress;
		}
 
		set
		{
			try
			{
				_macaddress = value;
			}
			catch
			{
				_macaddress = null;
			}
		}
	}
 
	/// <summary>
	/// get or set adapter description
	/// </summary>
	public String Description
	{
		get
		{
			return _description;
		}
 
		set
		{
			_description = value;
		}
	}
}
Categories: C# Tags: , ,
%d bloggers like this: