I am looking to find out what my IP address is from a console application.
I am using a web application using the Request.ServerVariables and / or Request.UserHostAddress .
Request.ServerVariables
Request.UserHostAddress
How can this be done in a console application?
The easiest way to do this:
using System; using System.Net; namespace ConsoleTest { class Program { static void Main() { String strHostName = string.Empty; // Getting Ip address of local machine... // First get the host name of local machine. strHostName = Dns.GetHostName(); Console.WriteLine("Local Machine Host Name: " + strHostName); // Then using host name, get the IP address list.. IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); IPAddress[] addr = ipEntry.AddressList; for (int i = 0; i < addr.Length; i++) { Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString()); } Console.ReadLine(); } } }
IPAddress [] addresslist = Dns.GetHostAddresses (Dns.GetHostName ());
The System.Net namespace is your friend here. In particular, APIs such as DNS.GetHostByName.
However, any computer can have multiple IP addresses (multiple network adapters, IPv4 and IPv6, etc.), so it is not as simple as you imagine.
Try the following:
String strHostName = Dns.GetHostName(); Console.WriteLine("Host Name: " + strHostName); // Find host by name IPHostEntry iphostentry = Dns.GetHostByName(strHostName); // Enumerate IP addresses int nIP = 0; foreach(IPAddress ipaddress in iphostentry.AddressList) { Console.WriteLine("IP #" + ++nIP + ": " + ipaddress.ToString()); }
using System; using System.Net; public class DNSUtility { public static int Main (string [] args) { String strHostName = new String (""); if (args.Length == 0) { // Getting Ip address of local machine... // First get the host name of local machine. strHostName = DNS.GetHostName (); Console.WriteLine ("Local Machine Host Name: " + strHostName); } else { strHostName = args[0]; } // Then using host name, get the IP address list.. IPHostEntry ipEntry = DNS.GetHostByName (strHostName); IPAddress [] addr = ipEntry.AddressList; for (int i = 0; i < addr.Length; i++) { Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ()); } return 0; } }
source: http://www.codeproject.com/KB/cs/network.aspx
System.Net.Dns.GetHostAddresses () should do this.
Source: https://habr.com/ru/post/927316/More articles:Separation seq - Recursion in Clojure (or Lisp in general) - lispusing newtonsoft, how to deserialize without knowing the type until runtime? - jsonGet all IP addresses assigned to the server - c #What is the use of the Align and Anchor properties on TForm? - alignmentCreate folder if it does not exist on Google Drive - javahttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/927317/are-there-any-high-level-explanations-of-how-zend-framework-2-all-fits-together&usg=ALkJrhi_JbeovuPBNzCCPZTkjv7NY0YCZgHow to pass variables to django via url? - pythonhttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/927319/convert-java-floats-to-c&usg=ALkJrhhYjRpvHyHrjjrUEmJnVRejTnZDzQBest way to serialize a float in java for reading with a C ++ application? - javaWhat is the maximum size of an ItemView in EWS? - c #All Articles