Friday, May 15, 2009

How to get Gateway in .NET

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;

public class ClsGatewayID
{
private static string _GatewayID = string.Empty;

public static string GatewayID
{
get
{
return mGetGatewayID();
}
}

private static string mGetGatewayID()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
if (addresses.Count > 0)
{
foreach (GatewayIPAddressInformation address in addresses)
{

if(string.IsNullOrEmpty(_GatewayID))
_GatewayID += address.Address.ToString();
else
_GatewayID += address.Address.ToString() + "\r\n";

}
}
}

return _GatewayID;
}
}

No comments:

Post a Comment

 
Locations of visitors to this page