using System;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Globalization;
namespace Framework
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Manager network.
////////////////////////////////////////////////////////////////////////////////////////////////////
public class ManagerNetwork
{
#region Gestion du singleton
/// The instance.
private static ManagerNetwork instance = null;
/// The padlock.
private static readonly object padlock = new object();
////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Prevents a default instance of the class from being created.
///
////////////////////////////////////////////////////////////////////////////////////////////////////
ManagerNetwork()
{
//if (_Log.IsDebugEnabled)
// _Log.DebugFormat("Nouvelle instance de {0}", Assembly.GetExecutingAssembly());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets the instance.
///
/// The instance.
////////////////////////////////////////////////////////////////////////////////////////////////////
public static ManagerNetwork Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new ManagerNetwork();
}
return instance;
}
}
}
#endregion Gestion du singleton
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets the MAC address.
///
/// The MAC address.
////////////////////////////////////////////////////////////////////////////////////////////////////
public String GetMacAddress()
{
var macAddr =
(
from nic in NetworkInterface.GetAllNetworkInterfaces()
where nic.OperationalStatus == OperationalStatus.Up
select nic.GetPhysicalAddress().ToString()
).FirstOrDefault();
return macAddr.ToString(CultureInfo.CurrentCulture);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets the Ip6 address.
///
/// The IP 6 address.
////////////////////////////////////////////////////////////////////////////////////////////////////
public String GetIP6Address()
{
string iP6Address = String.Empty;
try
{
foreach (IPAddress ipa in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (ipa.AddressFamily == AddressFamily.InterNetworkV6)
{
iP6Address = ipa.ToString();
break;
}
}
}
catch (Exception ex)
{
throw;
}
return iP6Address;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets the Ip4 address.
///
/// The IP 4 address.
////////////////////////////////////////////////////////////////////////////////////////////////////
public String GetIP4Address()
{
string iP4Address = String.Empty;
try
{
foreach (IPAddress ipa in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (ipa.AddressFamily == AddressFamily.InterNetwork)
{
iP4Address = ipa.ToString();
break;
}
}
}
catch (Exception ex)
{
throw;
}
return iP4Address;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets host name of the local computer.
///
/// The local host name.
////////////////////////////////////////////////////////////////////////////////////////////////////
public String GetLocalHostName()
{
return System.Net.Dns.GetHostName();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets a value indicating whether this instance is connected to internet.
///
/// true if this object is connected to internet, false if not.
////////////////////////////////////////////////////////////////////////////////////////////////////
public Boolean IsConnectedToInternet
{
get
{
try
{
HttpWebRequest hwebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
hwebRequest.Timeout = 10000;
HttpWebResponse hWebResponse = (HttpWebResponse)hwebRequest.GetResponse();
if (hWebResponse.StatusCode == HttpStatusCode.OK)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw;
}
}
}
}
}