How can I get the computer name and IP address of my PC programmatically? For example, I want to display that information in a text box.
Asked
Active
Viewed 3.7k times
12
-
http://stackoverflow.com/questions/1069103/how-to-get-my-own-ip-address-in-c – djeeg Jan 24 '11 at 05:19
-
Duplicate of [How to get the IP address of the server on which my C# application is running on?](https://stackoverflow.com/questions/1069103/how-to-get-the-ip-address-of-the-server-on-which-my-c-sharp-application-is-runni) – TylerH Sep 02 '20 at 20:29
4 Answers
25
Have a look at this: link
and this: link
textBox1.Text = "Computer Name: " + Environment.MachineName
textBox2.Text = "IP Add: " + Dns.GetHostAddresses(Environment.MachineName)[0].ToString();

Rye
- 2,273
- 9
- 34
- 43
-
get the ipaddress of your own machine is passible, how to get other machine ipaddress, while they login our application? – Prince Antony G Apr 19 '12 at 06:12
-
`Dns.GetHostAddresses(Environment.MachineName)` simply returns an array of all addresses for the current host. Picking `[0]` of that array is not helpful--especially when the host can have both IPv4 and IPv6 addresses. This doesn't even factor in private (internal network) vs public (external network) addresses. Forget about if the host is using > 0 VPNs. Picking the first element of this array is useless. – Christopher Oct 18 '18 at 04:34
-
Redact my comments in full--the concerns have already been addressed here: https://stackoverflow.com/questions/1069103/how-to-get-the-ip-address-of-the-server-on-which-my-c-sharp-application-is-runni – Christopher Oct 18 '18 at 04:43
3
Check more about this : How To Get IP Address Of A Machine
System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
string strName = p.Identity.Name;
To get the machine name,
System.Environment.MachineName
or
using System.Net;
strHostName = DNS.GetHostName ();
// 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 ());
}

Pranay Rana
- 175,020
- 35
- 237
- 263
1
In easy way..
string IP_Address = Dns.GetHostByName(Environment.MachineName).AddressList[0].toString();

Jenish Zinzuvadiya
- 999
- 2
- 15
- 29
0
I use the following found at: https://stackoverflow.com/a/27376368/2510099 for the IP address
public string GetIPAddress()
{
string ipAddress = null;
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
socket.Connect("8.8.8.8", 65530); //Google public DNS and port
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
ipAddress = endPoint.Address.ToString();
}
return ipAddress;
}
and for the machine name
Environment.MachineName;

DzSoundNirvana
- 81
- 5
-
This is a very clever idea, and I think it would work for a lot of folks. If, however, you want to know the private IP address that your system is using to communicate between other systems, this technique will not help. – Christopher Oct 18 '18 at 04:40