469,268 Members | 920 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

How to get local machine name and IP address?

Hi, here,
How to get local machine name and IP address?
Thanks.

Nov 29 '06 #1
5 72016
go to the command prompt (Start -Run -type "cmd")
at the prompt type "ipconfig"

"ipconfig/all" for more details.

for the computer name rightclick on MyComputer and go to properties,
then computer name.

ta-da.
Hooyoo wrote:
Hi, here,
How to get local machine name and IP address?
Thanks.
Nov 29 '06 #2

jayper wrote:
go to the command prompt (Start -Run -type "cmd")
at the prompt type "ipconfig"

"ipconfig/all" for more details.

for the computer name rightclick on MyComputer and go to properties,
then computer name.

ta-da.
Hooyoo wrote:
Hi, here,
How to get local machine name and IP address?
Thanks.
faint, I mean write codes to get that.

Nov 29 '06 #3
Environment.MachineName will return the local NetBios name as a string

you can also use:

System.Net.dns.GetHostName();

Getting the IP addresses is a little more tricky - as there can be more
than one per host name:

System.Net.IPAddress[] a =
System.Net.Dns.GetHostAddresses(System.Net.Dns.Get HostName());

for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i].ToString());
}

Hope this solves your problem

On Nov 29, 2:07 pm, "Hooyoo" <zhao_huy...@126.comwrote:
jayper wrote:
go to the command prompt (Start -Run -type "cmd")
at the prompt type "ipconfig"
"ipconfig/all" for more details.
for the computer name rightclick on MyComputer and go to properties,
then computer name.
ta-da.
Hooyoo wrote:
Hi, here,
How to get local machine name and IP address?
Thanks.faint, I mean write codes to get that.
Nov 29 '06 #4
Hi,

Use Environment.MachineName to get the local machine's NetBIOS name.

Here's some code that will get one of the IP addresses assigned to the local
computer, accounting for multiple network adapters and scope (e.g., LAN,
WAN):

public static class Network
{
#region DNS
public static IPAddress FindIPAddress(bool localPreference)
{
return FindIPAddress(Dns.GetHostEntry(Dns.GetHostName()),
localPreference);
}

public static IPAddress FindIPAddress(IPHostEntry host, bool
localPreference)
{
if (host == null)
throw new ArgumentNullException("host");

if (host.AddressList.Length == 1)
return host.AddressList[0];
else
{
foreach (System.Net.IPAddress address in host.AddressList)
{
bool local = IsLocal(address);

if (local && localPreference)
return address;
else if (!local && !localPreference)
return address;
}

return host.AddressList[0];
}
}

public static bool IsLocal(IPAddress address)
{
if (address == null)
throw new ArgumentNullException("address");

byte[] addr = address.GetAddressBytes();

return addr[0] == 10
|| (addr[0] == 192 && addr[1] == 168)
|| (addr[0] == 172 && addr[1] >= 16 && addr[1] <= 31);
}
#endregion
}

--
Dave Sexton

"Hooyoo" <zh*********@126.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
Hi, here,
How to get local machine name and IP address?
Thanks.

Nov 29 '06 #5
Thanks, got it.
ni***********@iinet.net.au wrote:
Environment.MachineName will return the local NetBios name as a string

you can also use:

System.Net.dns.GetHostName();

Getting the IP addresses is a little more tricky - as there can be more
than one per host name:

System.Net.IPAddress[] a =
System.Net.Dns.GetHostAddresses(System.Net.Dns.Get HostName());

for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i].ToString());
}

Hope this solves your problem

On Nov 29, 2:07 pm, "Hooyoo" <zhao_huy...@126.comwrote:
jayper wrote:
go to the command prompt (Start -Run -type "cmd")
at the prompt type "ipconfig"
"ipconfig/all" for more details.
for the computer name rightclick on MyComputer and go to properties,
then computer name.
ta-da.
Hooyoo wrote:
Hi, here,
How to get local machine name and IP address?
Thanks.faint, I mean write codes to get that.
Nov 29 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Robin Tucker | last post: by
1 post views Thread by Chakravarti Mukesh | last post: by
6 posts views Thread by Michael | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.