標籤:
在.NET中擷取一台電腦名,IP地址及目前使用者名是非常簡單,以下是我常用的幾種方法,如果大家還有其它好的方法,可以回複一起整理: 1. 在ASP.NET中專用屬性: 擷取伺服器電腦名:Page.Server.ManchineName 擷取使用者資訊:Page.User 擷取用戶端電腦名:Page.Request.UserHostName 擷取用戶端電腦IP:Page.Request.UserHostAddress 2. 在網路編程中的通用方法: 擷取當前電腦名:static System.Net.Dns.GetHostName() 根據電腦名取出全部IP地址:static System.Net.Dns.Resolve(電腦名).AddressList 也可根據IP地址取出電腦名:static System.Net.Dns.Resolve(IP地址).HostName 3. 系統內容類的通用屬性: 當前電腦名:static System.Environment.MachineName 當前電腦所屬網域:static System.Environment.UserDomainName 當前電腦使用者:static System.Environment.UserNameRequest.UserHostAddressstring IP = (Request.ServerVariables["HTTP_VIA"]!=null)?Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString():Request.ServerVariables["REMOTE_ADDR"].ToString();string ipAddress = System.Web.HttpContext.Current.Request.UserHostAddress;先引用命名空間:using System.Net; 得到用戶端IP地址:Request.ServerVariables["REMOTE_ADDR"]或Rqurest.UserHostAddress(); 得到本機IP地址:Request.ServerVariables["local_addr"]或addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ; HttpBrowserCapabilities bc= Request.Browser; Label3.Text=bc.Browser+" "+bc.Version;//得到瀏覽器版本 Label2.Text=bc.Platform;//得到作業系統 hostname=Dns.GetHostName(); IPHostEntry jj=Dns.GetHostByName(hostname);//得到IP //IPAddress ip=Dns.Resolve(hostname); Label1.Text=jj.AddressList[0].ToString();
.NET中擷取IP地址