How to get local machine name and IP address?

  • Thread starter Thread starter Hooyoo
  • Start date Start date
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.
 
jayper said:
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.

faint, I mean write codes to get that.
 
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.GetHostName());

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

Hope this solves your problem
 
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
}
 
Thanks, got it.
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.GetHostName());

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

Hope this solves your problem

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top