获取使用C#计算机名称 [英] Getting computers name using c#

查看:170
本文介绍了获取使用C#计算机名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CS文件这个code到一个asp.net页面从登录的用户获取IP地址:

<$p$p><$c$c>cmd.Parameters.AddWithValue(\"@ip\",System.Web.HttpContext.Current.Request.ServerVariables[\"REMOTE_ADDR\"]);

我想也有他的计算机的名称,以及。我该怎么做呢?


解决方案

这未必是可行的。

您可以尝试扭转从IP地址查找名称,使用类似

 私人的String [] GetHostnamesForIpAddress(字符串ip地址)
{
   VAR HOSTIP = IPAddress.Parse(ip地址);
   IPHostEntry Hostinfo中= Dns.GetHostByAddress(主机IP);   返回hostInfo.Alias​​es;
}

在一个本地网络(其中客户端是本地的你,例如在企业网络中),这很可能是好的,只要所有客户端在DNS反向IP映射。

在互联网上,这是不太可能对大多数客户的工作。你只需要IP地址去了,一般这些不会有反向DNS映射设置。事实上,一个可怕的很多机器在那里将是代理服务器和网关的NAT后面,只有私人的,不可路由的IP地址,以便您不可能做一个反向查找。

I am using this code in cs file into an asp.net page for getting ip address from the logged user:

cmd.Parameters.AddWithValue("@ip",System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);

I would like to have also his computer name, as well. How do I do that?

解决方案

This is not necessarily possible.

You can attempt to reverse lookup the name from the ip address, using something like

private string[] GetHostnamesForIpAddress(string ipAddress)
{
   var hostIp= IPAddress.Parse(ipAddress);
   IPHostEntry hostInfo = Dns.GetHostByAddress(hostIp);

   return hostInfo.Aliases;
}

On a local network (where your client is local to you, e.g. on a corporate network), this may well be ok, as long as all the clients have reverse ip mappings in DNS.

Over the internet, it is far less likely to work for most clients. You only have the IP address to go on, and generally these won't have reverse DNS mappings set up. In fact, an awful lot of machines out there will be behind proxies and NAT gateways and only have private, non-routable ip addresses, for which you can't possibly do a reverse lookup.

这篇关于获取使用C#计算机名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆