I'm trying to get the Physical Address of the user's computer but having no luck. I developed a method that was working but isn't anymore. I'm also having trouble getting the IP Addresses Broadcast and Loopback.

string ip = IPAddress.Broadcast.Address.ToString();
string ipl = IPAddress.Loopback.Address.ToString();

The physical address is the one I'm having the most difficulty with.

Here are some network extensions that should help you out:

static IEnumerable<UnicastIPAddressInformation> GetLocalIPv4Bindings()
    {
      NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
      foreach (NetworkInterface ni in interfaces)
      {
        IPInterfaceProperties props;
        try
        {
          props = ni.GetIPProperties();
        }
        catch
        {
          continue;
        }

        for (int i1 = 0; i1 < props.UnicastAddresses.Count; i1++)
        {
          if (props.UnicastAddresses[i1].Address.AddressFamily == AddressFamily.InterNetwork)
            yield return props.UnicastAddresses[i1];
        }
      }
    }
    public static bool BelongsToSubnet(this IPAddress @this, IPAddress subnetAddress, IPAddress subnetMask)
    {
      if (subnetAddress == null) throw new ArgumentNullException("subnetAddress");
      if (subnetMask == null) throw new ArgumentNullException("subnetMask");
      if (@this.AddressFamily != AddressFamily.InterNetwork) throw new NotImplementedException(@this.AddressFamily.ToString());
      if (subnetAddress.AddressFamily != AddressFamily.InterNetwork) throw new NotImplementedException(subnetAddress.AddressFamily.ToString());
      if (subnetMask.AddressFamily != AddressFamily.InterNetwork) throw new NotImplementedException(subnetMask.AddressFamily.ToString());
      uint uMatch = BitConverter.ToUInt32(@this.GetAddressBytes(), 0);
      uint uSubnetAddress = BitConverter.ToUInt32(subnetAddress.GetAddressBytes(), 0);
      uint uSubnetMask = BitConverter.ToUInt32(subnetMask.GetAddressBytes(), 0);
      return ((uSubnetAddress & uSubnetMask) == (uMatch & uSubnetMask));
    }
    public static IPAddress CalculateBroadcastAddress(this IPAddress @this, IPAddress subnetMask)
    {
      if (@this == null) throw new ArgumentNullException("this");
      if (subnetMask == null) throw new ArgumentNullException("subnetMask");
      if (@this.AddressFamily != AddressFamily.InterNetwork) throw new NotImplementedException(@this.AddressFamily.ToString());
      if (subnetMask.AddressFamily != AddressFamily.InterNetwork) throw new NotImplementedException(subnetMask.AddressFamily.ToString());

      uint uAddr = BitConverter.ToUInt32(@this.GetAddressBytes(), 0);
      uint uSubnet = BitConverter.ToUInt32(subnetMask.GetAddressBytes(), 0);
      uint uBroadcast = uAddr | ~uSubnet;
      return new IPAddress(BitConverter.GetBytes(uBroadcast));
    }
    public static bool BelongsToLocalSubnet(this IPAddress @this)
    {
      try
      {
        if (@this.AddressFamily != AddressFamily.InterNetwork)
          throw new NotImplementedException();
        uint match = BitConverter.ToUInt32(@this.GetAddressBytes(), 0);
        UnicastIPAddressInformation[] localBindings = GetLocalIPv4Bindings().ToArray();
        for (int i1 = 0; i1 < localBindings.Length; i1++)
        {
          UnicastIPAddressInformation uni = localBindings[i1];
          if ((uni.Address == null) || (uni.IPv4Mask == null)) continue;
          if (@this.BelongsToSubnet(uni.Address, uni.IPv4Mask))
            return true;
        }
      }
      catch { }
      return false;
    }

You can expand enumerating IPv4 bindings to IPv6 and grab the physical address from the network adapter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.