How to get local machine name and IP address?
2008-11-20 11:18
525 查看
THANKS nick.fletcher@iinet.net.au and Dave Sexton, your notes give me a lot of help.
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:
public static class Network
{
public static string GetMachineName()
{
//get domain and machine name;
//return System.Net.Dns.GetHostName();
//just get the local machine's NetBIOS name.
return Environment.MachineName;
}
public static string GetUserName()
{
return HttpContext.Current.User.Identity.Name;
}
#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
}
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:
public static class Network
{
public static string GetMachineName()
{
//get domain and machine name;
//return System.Net.Dns.GetHostName();
//just get the local machine's NetBIOS name.
return Environment.MachineName;
}
public static string GetUserName()
{
return HttpContext.Current.User.Identity.Name;
}
#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
}
相关文章推荐
- Linux: Where to set hostname and how to get IP info, as well as how to set DNS
- How to get browser name, version and operating system detail in selenium 2(Web driver)
- How to Configure Network Static IP Address and Manage Services on RHEL/CentOS 7.0
- How to use script to get all oracle EBS Form name and corresponding fmb file name
- how to get url and hostname and url text in one page
- [转]How to convert IP address to country name
- on IRC, how to use secure connection(SSL) and get a cloak/vhost to hide your IP
- How to set up ADB for remote machine development and local device deployment
- [笔记] How to get the versionCode and versionName of your app
- How to Use Oradebug to Get Trace File Name and Location
- How to get xen whole-machine dump image and analyse it prepair
- How To Get Application Path,And How To Get The Path's Directory,File Name,And Type Info(如何得到运行程序的路径,以及如何得到路径的文件夹,文件名,以及类型等等信息)
- How to use script to get all Oracle EBS Concurrent Program/Report name and corresponding execution f
- How to Get App Name and Version
- How do I use a host name to look up an IP address?
- How to get local machine date format.如何获取本机时间格式
- Get Local IP Address and Set in the IP Control
- How to dd a remote disk using SSH on local machine and save to a local disk
- linux上获得本地IP地址和MAC地址(get local ip address and mac address)
- How to Configure Static IP Address on CentOS 6.3 Linux Server