Retrieve IP address of device

I am using Xamarin.Android and wrote the following code:

public TextView text; text = FindViewById<TextView>(Resource.Id.viewIP); foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName())) { text.Text = "IP Adress: " + adress; }

However, when I open the application it shuts down immediately. Am I using the correct way of getting the IP address' of the device?


From the Xamarin forums

Java.Util.IEnumeration networkInterfaces = NetworkInterface.NetworkInterfaces; while(networkInterfaces.HasMoreElements) { Java.Net.NetworkInterface netInterface = (Java.Net.NetworkInterface)networkInterfaces.NextElement(); Console.WriteLine(netInterface.ToString()); }


added to mainifest:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" />

for get local ip:

public static string GetLocalIPAddress() { var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { return ip.ToString(); } } throw new Exception("Local IP Address Not Found!"); }

See this answer : https://stackoverflow.com/a/6803109/4349342


For me this worked in PCL Xamarin:

public static string GetIPAddress() { var AllNetworkInterfaces = Collections.List(Java.Net.NetworkInterface.NetworkInterfaces); var IPAddres = ""; foreach (var interfaces in AllNetworkInterfaces) { if (!(interfaces as Java.Net.NetworkInterface).Name.Contains("eth0")) continue; var AddressInterface = (interfaces as Java.Net.NetworkInterface).InterfaceAddresses; foreach (var AInterface in AddressInterface) { if(AInterface.Broadcast != null) IPAddres = AInterface.Address.HostAddress; } } return IPAddres; }


All the answers I've seen to this question have only gotten the internal IP address of my device while on my home network (198.162.#.#). So I took a slightly different approach, and ask the internet more directly. ipify.org has a nice and simple endpoint for getting your IP address, that can be executed in your shared code. For example...

var client = new HttpClient(); var response = await client.GetAsync("https://api.ipify.org/?format=json"); var resultString = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject<IpResult>(resultString); var yourIp = result.Ip;

Where "IpResult" is a POCO with a single string property named "Ip" (that you need to create, in addition to this code.)


  • Cycle through an array on click
  • com.google.ads.AdView : The following classes could not be found
  • How to Get IBM i Job details Using Job Number - Java ( JT400 )
  • Mail repeatedly sent by JavaMail ends up in Spam folder
  • (Java/Android) Client-side Socket throwing IOException
  • Better Code Using Jacob and WMI
  • Split a string while preserving values wrapped in quotes (.NET)
  • Shuffling a 2D array without using Collections
  • C# where to add a method
  • VB.NET 2012 Property Set on Property Get
  • Separate ID and Class for JS and CSS
  • auth.provider is not set to 'password' when user signs-in with email and password
  • Draw half infinite lines?
  • php is_file always return false
  • ckeditor and jquery UI dialog not working
  • Vuejs: Lifecycle hooks of child routerview components using keep alive
  • What causes the runtime difference in this trivial fortran code?
  • Retrieve IP address of device
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Implicit joins and Where in Doctrine - how?
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • cygwin cannot exec 'git-add--interactive' permission denied
  • Adding elements to a huge XML file
  • Conversion from string “a” to type 'Boolean' is not valid
  • How to define and use opencv mat of user type
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • How to set ini file attributes during an Inno install
  • Volley JsonObjectRequest send headers in GET Request
  • Cassandra Data Model
  • Javascript convert timezone issue
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • How to pass list parameters for each object using Spring MVC?
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Proper way to use connect-multiparty with express.js?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • Python/Django TangoWithDjango Models and Databases