Gets my local IP address (not necessarily external) and subnet mask - CSharp System.Net

CSharp examples for System.Net:IP Address

Description

Gets my local IP address (not necessarily external) and subnet mask

Demo Code

/* Copyright (c) 2010 Michael Lidgren

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software./* w  w  w .  j  av  a 2 s . c o m*/

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System.Text.RegularExpressions;
using System.Text;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Net;
using System;

public class Main{
        /// <summary>
      /// Gets my local IP address (not necessarily external) and subnet mask
      /// </summary>
      public static IPAddress GetMyAddress(out IPAddress mask)
      {
         mask = null;
#if ANDROID
         try
         {
            Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)Android.App.Application.Context.GetSystemService(Android.App.Activity.WifiService);
            if (!wifi.IsWifiEnabled) return null;
            var dhcp = wifi.DhcpInfo;
               
            int addr = dhcp.IpAddress;
             byte[] quads = new byte[4];
             for (int k = 0; k < 4; k++)
            {
                  quads[k] = (byte) ((addr >> k * 8) & 0xFF);
            }         
            return new IPAddress(quads);
         }
         catch // Catch Access Denied errors
         {
         }
            
#endif         
#if IS_FULL_NET_AVAILABLE
         NetworkInterface ni = GetNetworkInterface();
         if (ni == null)
         {
            mask = null;
            return null;
         }

         IPInterfaceProperties properties = ni.GetIPProperties();
         foreach (UnicastIPAddressInformation unicastAddress in properties.UnicastAddresses)
         {
            if (unicastAddress != null && unicastAddress.Address != null && unicastAddress.Address.AddressFamily == AddressFamily.InterNetwork)
            {
#if !MONOMAC
               mask = unicastAddress.IPv4Mask;
#endif
               return unicastAddress.Address;
            }
         }
#endif
         return null;
      }
}

Related Tutorials