You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.7 KiB

6 years ago
  1. using System.Net;
  2. using System.Net.NetworkInformation;
  3. using System.Net.Sockets;
  4. public class IPGrabber{
  5. public static string GetIP(ADDRESSFAM Addfam)
  6. {
  7. //Return null if ADDRESSFAM is Ipv6 but Os does not support it
  8. if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
  9. {
  10. return null;
  11. }
  12. string output = "";
  13. foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
  14. {
  15. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  16. NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
  17. NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;
  18. if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
  19. #endif
  20. {
  21. foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
  22. {
  23. //IPv4
  24. if (Addfam == ADDRESSFAM.IPv4)
  25. {
  26. if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
  27. {
  28. output = ip.Address.ToString();
  29. }
  30. }
  31. //IPv6
  32. else if (Addfam == ADDRESSFAM.IPv6)
  33. {
  34. if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
  35. {
  36. output = ip.Address.ToString();
  37. }
  38. }
  39. }
  40. }
  41. }
  42. return output;
  43. }
  44. }
  45. public enum ADDRESSFAM
  46. {
  47. IPv4, IPv6
  48. }