ASP.NET獲取客戶端及伺服器的資訊
客戶端資訊:
-
在ASP.NET中專用屬性: 獲取伺服器電腦名:Page.Server.ManchineName 獲取使用者資訊:Page.User 獲取客戶端電腦名:Page.Request.UserHostName 獲取客戶端電腦IP:Page.Request.UserHostAddress
-
在網路程式設計中的通用方法: 獲取當前電腦名:static System.Net.Dns.GetHostName() 根據電腦名取出全部IP地址:static System.Net.Dns.Resolve(電腦名).AddressList 也可根據IP地址取出電腦名:static System.Net.Dns.Resolve(IP地址).HostName
-
系統環境類的通用屬性: 當前電腦名:static System.Environment.MachineName 當前電腦所屬網域:static System.Environment.UserDomainName 當前電腦使用者:static System.Environment.UserName
客戶端IP:Page.Request.UserHostAddress;
使用者資訊:Page.User;
伺服器電腦名稱:Page.Server.MachineName;
當前使用者電腦名稱:System.Net.Dns.GetHostName();
當前電腦名:System.Environment.MachineName;
當前電腦所屬網域:System.Environment.UserDomainName;
當前電腦使用者:System.Environment.UserName;
瀏覽器型別:Request.Browser.Browser;
瀏覽器標識:
伺服器端的資訊:
伺服器計算機名:“http://” + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
伺服器IIS版本: Request.ServerVariables[“Server_SoftWare”].ToString();
伺服器域名:Request.ServerVariables[“SERVER_NAME”].ToString();
伺服器埠:Request.ServerVariables[“Server_Port”].ToString();
伺服器IP地址:Request.ServerVariables[“LOCAl_ADDR”]
伺服器指令碼超時時間:(Server.ScriptTimeout / 1000).ToString() + “秒”;
伺服器作業系統:Environment.OSVersion.ToString();
本檔案所在資料夾:Request.PhysicalApplicationPath;
伺服器IE版本:Registry.LocalMachine.OpenSubKey(@“SOFTWARE\Microsoft\Internet Explorer\Version Vector”).GetValue(“IE”, “未檢測到”).ToString();
系統所在資料夾:Environment.SystemDirectory.ToString();
伺服器當前時間: DateTime.Now.ToString();
伺服器的語言種類:CultureInfo.InstalledUICulture.EnglishName;
伺服器上次啟動到現在已執行時間: ((Environment.TickCount / 0x3e8) / 60).ToString() + “分鐘”;
CPU 型別:Environment.GetEnvironmentVariable(“PROCESSOR_IDENTIFIER”).ToString();
邏輯驅動器:string[] achDrives = Directory.GetLogicalDrives(); for (int i = 0; i < Directory.GetLogicalDrives().Length - 1; i++) { achDrives[i].ToString(); }
CPU 總數:Environment.GetEnvironmentVariable(“NUMBER_OF_PROCESSORS”).ToString();
虛擬記憶體:(Environment.WorkingSet / 1024).ToString() + “M”;
.NET Framework 版本:string.Concat(new object[] { Environment.Version.Major, “.”, Environment.Version.Minor, Environment.Version.Build, “.”, Environment.Version.Revision });
Asp.net所佔CPU:((TimeSpan)Process.GetCurrentProcess().TotalProcessorTime).TotalSeconds.ToString(“N0”);
Asp.net所佔記憶體: ((Double)Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString(“N2”) + “M”;
當前Session數量:Session.Contents.Count.ToString();
當前程式佔用記憶體:((Double)GC.GetTotalMemory(false) / 1048576).ToString(“N2”) + “M”;
當前SessionID:Session.Contents.SessionID;
當前系統使用者名稱:Environment.UserName;
示例程式碼:獲取伺服器端資訊
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "伺服器名稱:" + Server.MachineName;//伺服器名稱
Label2.Text = "伺服器IP地址:" + Request.ServerVariables["LOCAL_ADDR"];//伺服器IP地址
Label3.Text = "伺服器域名:" + Request.ServerVariables["SERVER_NAME"];//伺服器域名
Label4.Text = ".NET解釋引擎版本:" + ".NET CLR" + Environment.Version.Major + "." + Environment.Version.Minor + "." + Environment.Version.Build + "." + Environment.Version.Revision;//.NET解釋引擎版本
Label5.Text = "伺服器作業系統版本:" + Environment.OSVersion.ToString();//伺服器作業系統版本
Label6.Text = "伺服器IIS版本:" + Request.ServerVariables["SERVER_SOFTWARE"];//伺服器IIS版本
Label7.Text = "HTTP訪問埠:" + Request.ServerVariables["SERVER_PORT"];//HTTP訪問埠
Label8.Text = "虛擬目錄的絕對路徑:" + Request.ServerVariables["APPL_RHYSICAL_PATH"];//虛擬目錄的絕對路徑
Label9.Text = "執行檔案的絕對路徑:" + Request.ServerVariables["PATH_TRANSLATED"];//執行檔案的絕對路徑
Label10.Text = "虛擬目錄Session總數:" + Session.Contents.Count.ToString();//虛擬目錄Session總數
Label11.Text = "虛擬目錄Application總數:" + Application.Contents.Count.ToString();//虛擬目錄Application總數
Label12.Text = "域名主機:" + Request.ServerVariables["HTTP_HOST"];//域名主機
Label13.Text = "伺服器區域語言:" + Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];//伺服器區域語言
Label14.Text = "使用者資訊:" + Request.ServerVariables["HTTP_USER_AGENT"];
Label14.Text = "CPU個數:" + Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS");//CPU個數
Label15.Text = "CPU型別:" + Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER");//CPU型別
Label16.Text = "程序開始時間:" + GetPrStart();//程序開始時間
Label17.Text = "AspNet 記憶體佔用:" + GetAspNetN();//AspNet 記憶體佔用
Label18.Text = "AspNet CPU時間:" + GetAspNetCpu();//AspNet CPU時間
Label19.Text = "FSO 文字檔案讀寫:" + Check("Scripting.FileSystemObject");//FSO 文字檔案讀寫
Label20.Text = "應用程式佔用記憶體" + GetServerAppN();//應用程式佔用記憶體
}
#region 獲取伺服器系統資訊
/// <summary>
/// 應用程式佔用記憶體
/// </summary>
/// <returns></returns>
private string GetServerAppN()
{
string temp;
try
{
temp = ((Double)GC.GetTotalMemory(false) / 1048576).ToString("N2") + "M";
}
catch
{
temp = "未知";
}
return temp;
}
/// <summary>
/// AspNet 記憶體佔用
/// </summary>
/// <returns></returns>
private string GetAspNetN()
{
string temp;
try
{
temp = ((Double)System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + "M";
}
catch
{
temp = "未知";
}
return temp;
}
/// <summary>
/// AspNet CPU時間
/// </summary>
/// <returns></returns>
private string GetAspNetCpu()
{
string temp;
try
{
temp = ((TimeSpan)System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime).TotalSeconds.ToString("N0");
}
catch
{
temp = "未知";
}
return temp;
}
/// <summary>
/// 程序開始時間
/// </summary>
/// <returns></returns>
private string GetPrStart()
{
string temp;
try
{
temp = System.Diagnostics.Process.GetCurrentProcess().StartTime.ToString();
}
catch
{
temp = "未知";
}
return temp;
}
/// <summary>
/// FSO 文字檔案讀寫
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private string Check(string obj)
{
try
{
object claobj = Server.CreateObject(obj);
return "支援";
}
catch
{
return "不支援";
}
}
#endregion
程式碼如下:
Request.RawUrl:獲取客戶端請求的URL資訊(不包括主機和埠)------>/Default2.aspx Request.ApplicationPath:獲取伺服器上ASP.NET應用程式的虛擬路徑。------>/ Request.CurrentExecutionFilePath:獲取當前請求的虛擬路徑。------>/Default2.aspx Request.Path:獲取當前請求的虛擬路徑。------>/Default2.aspx Request.PathInfo:取具有URL副檔名的資源的附加路徑資訊------> Request.PhysicalPath:獲取與請求的URL相對應的物理檔案系統路徑。------>E:\temp\Default2.aspx Request.Url.LocalPath:------>/Default2.aspx Request.Url.AbsoluteUri:------>http://localhost:8080/Default2.aspx Request.Url.AbsolutePath:---------------------------->/Default2.aspx