In general, we are getting IP address by run command by type keyword ipconfig . And, if we execute this as Administrator Command Prompt then you will get the basic Network details like Host Name, IPAddress and Gateway.
In this example we can achieve both byGetHostName() and Dns.GetHostByName(hostname) Method using the namespace System.Net.
using System.Net;
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine(GetIPAddress());
}catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}private static string GetIPAddress()
{
string strHostName = null;
strHostName = System.Net.Dns.GetHostName();
string myIP = Dns.GetHostByName(strHostName).AddressList[0].ToString();
return "Host Name: " + strHostName + "; IP Address: " + myIP;
}}
Output:
your Host
your Ip Address
Post your comments / questions
Recent Article
- Fix-Gradient effect turning to gray in after effects
- How to blur an image in python?
- ModuleNotFoundError: No module named 'whois' in Python GoviralHost Without Terminal
- How to Convert Image to Pencil Sketch in Python?
- AttributeError: module 'urllib' has no attribute 'request' - Python
- How to Extract audio from video files using python?
- PermissionError: [Errno 13] Permission denied: 'shampoo_sales.csv' - Python
- [WinError 145] The directory is not empty: 'FolderPath' - Python
Related Article