121 lines
3.6 KiB
C#
121 lines
3.6 KiB
C#
using NLog;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
|
|
namespace IobManComm
|
|
{
|
|
public class baseUtils
|
|
{
|
|
#region Public Fields
|
|
|
|
/// <summary>
|
|
/// Indicazione VETO PING a server sino alla data-ora indicata
|
|
/// </summary>
|
|
public static DateTime dtVetoPing = DateTime.Now;
|
|
|
|
/// <summary>
|
|
/// Indicazione VETO invio a server sino alla data-ora indicata
|
|
/// </summary>
|
|
public static DateTime dtVetoSend = DateTime.Now;
|
|
|
|
/// <summary>
|
|
/// Status IOB
|
|
/// </summary>
|
|
public static bool IOB_Online = false;
|
|
|
|
/// <summary>
|
|
/// Classe logger
|
|
/// </summary>
|
|
public static Logger lg = LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
/// Status MP_IO
|
|
/// </summary>
|
|
public static bool MPIO_Online = false;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// IP della macchina
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string GetIP()
|
|
{
|
|
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
|
|
String sIpAddr = string.Empty;
|
|
try
|
|
{
|
|
foreach (NetworkInterface adapter in nics)
|
|
{
|
|
if (sIpAddr == String.Empty)// only return IP Address from first card
|
|
{
|
|
IPInterfaceProperties properties = adapter.GetIPProperties();
|
|
foreach (var item in properties.UnicastAddresses)
|
|
{
|
|
if (item.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
|
{
|
|
sIpAddr = item.Address.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lg.Error(exc);
|
|
}
|
|
return sIpAddr;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Macaddress della macchina
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string GetMACAddress()
|
|
{
|
|
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
|
|
String sMacAddress = string.Empty;
|
|
foreach (NetworkInterface adapter in nics)
|
|
{
|
|
if (sMacAddress == String.Empty)// only return MAC Address from first card
|
|
{
|
|
IPInterfaceProperties properties = adapter.GetIPProperties();
|
|
//sMacAddress = adapter.GetPhysicalAddress().ToString();
|
|
sMacAddress = string.Join(":", (from z in adapter.GetPhysicalAddress().GetAddressBytes() select z.ToString("X2")).ToArray());
|
|
}
|
|
}
|
|
return sMacAddress;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test ping x indirizzo indicato
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <returns></returns>
|
|
public static bool pingAddress(IPAddress address)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
Ping pingSender = new Ping();
|
|
PingReply reply = pingSender.Send(address, 100);
|
|
// se passa il ping do OK...
|
|
if (reply.Status == IPStatus.Success)
|
|
{
|
|
answ = true;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lg.Error(exc);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |