78 lines
2.5 KiB
C#
78 lines
2.5 KiB
C#
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IOB_UT_NEXT.Services.Networking
|
|
{
|
|
public class NetService
|
|
{
|
|
#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;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Internal Fields
|
|
|
|
/// <summary>
|
|
/// Classe logger
|
|
/// </summary>
|
|
internal static Logger lg = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Internal Fields
|
|
}
|
|
} |