convert VB to C#
This commit is contained in:
62
Pilz.Networking/NetworkFeatures.cs
Normal file
62
Pilz.Networking/NetworkFeatures.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System.Linq;
|
||||
using global::System.Net;
|
||||
using global::System.Net.NetworkInformation;
|
||||
using global::System.Net.Sockets;
|
||||
|
||||
namespace Pilz.Networking
|
||||
{
|
||||
public static class NetworkFeatures
|
||||
{
|
||||
public static IPAddress GetIPFromHost(string hostName)
|
||||
{
|
||||
return Dns.GetHostAddresses(hostName).FirstOrDefault(n => n.AddressFamily == AddressFamily.InterNetwork);
|
||||
}
|
||||
|
||||
public static object GetHostFromIP(string ip)
|
||||
{
|
||||
return Dns.GetHostEntry(ip)?.HostName;
|
||||
}
|
||||
|
||||
public static UnicastIPAddressInformation GetLocalIPInformations()
|
||||
{
|
||||
UnicastIPAddressInformation addr = null;
|
||||
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
|
||||
{
|
||||
if (addr is null && adapter.OperationalStatus == OperationalStatus.Up && adapter.NetworkInterfaceType != NetworkInterfaceType.Tunnel && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback)
|
||||
{
|
||||
foreach (UnicastIPAddressInformation uni in adapter.GetIPProperties().UnicastAddresses)
|
||||
{
|
||||
if (addr is null && uni.Address.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
addr = uni;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
public static IPAddress GetLocalIPAddress()
|
||||
{
|
||||
return GetLocalIPInformations()?.Address;
|
||||
}
|
||||
|
||||
public static IPAddress GetLocalIPv4Mask()
|
||||
{
|
||||
return GetLocalIPInformations()?.IPv4Mask;
|
||||
}
|
||||
|
||||
public static IPAddress GetLocalBoradcastIP(UnicastIPAddressInformation ipInfo)
|
||||
{
|
||||
IPAddress ip = null;
|
||||
var myIPBytes = ipInfo.Address.GetAddressBytes();
|
||||
var subnetBytes = ipInfo.IPv4Mask.GetAddressBytes();
|
||||
var broadcastBytes = new byte[myIPBytes.Length];
|
||||
for (int i = 0, loopTo = subnetBytes.Length - 1; i <= loopTo; i++)
|
||||
broadcastBytes[i] = (byte)(myIPBytes[i] | ~subnetBytes[i]);
|
||||
ip = new IPAddress(broadcastBytes);
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user