ASP.NET获取服务器信息大全
p>服务器IP:<%=Request.ServerVariables["LOCAL_ADDR"]%></p>
<p>服务器名:<%=Request.ServerVariables["SERVER_NAME"]%></p>
<p> HTTP端口:<%=Request.ServerVariables["SERVER_PORT"]%></p>
<p> 服务器时间:<%=DateTime .Now%></p>
<p> 操作系统信息:<%=Request.ServerVariables["HTTP_USER_AGENT"]%></p>
<p> 允许文件:<%=Request.ServerVariables["HTTP_ACCEPT"]%></p>
<p> 虚拟目录:<%=HttpContext.Current.Request.ApplicationPath%></p>
<p> 物理路径:<%=HttpRuntime.AppDomainAppPath%></p>
<p> 探针文件路径:<%=Context.Server.MapPath(Request.ServerVariables["SCRIPT_NAME"])%></p>
<p> 脚本超时时间:<%=Server.ScriptTimeout%>(秒)</p>
<p> CPU个数: <%=Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS")%></p>
String serverOS = Environment.OSVersion.ToString();
String CpuSum = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS");// CPU个数:
String CpuType = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER");// CPU类型:
String ServerSoft = Request.ServerVariables["SERVER_SOFTWARE"]; // 信息服务软件:
String MachineName = Server.MachineName;// 服务器名
String ServerName = Request.ServerVariables["SERVER_NAME"];// 服务器域名
String ServerPath = Request.ServerVariables["APPL_PHYSICAL_PATH"];// 虚拟服务绝对路径
String ServerNet = ".NET CLR " + Environment.Version.ToString(); // DotNET 版本
String ServerArea = (DateTime.Now - DateTime.UtcNow).TotalHours > 0 ? "+" + (DateTime.Now -DateTime.UtcNow).TotalHours.ToString() : (DateTime.Now - DateTime.UtcNow).TotalHours.ToString();// 服务器时区
String ServerTimeOut = Server.ScriptTimeout.ToString(); // 脚本超时时间
String ServerStart = ((Double)System.Environment.TickCount / 3600000).ToString("N2");// 开机运行时长
// AspNet CPU时间
String ServerSessions = Session.Contents.Count.ToString();// Session总数
String ServerApp = Application.Contents.Count.ToString(); // Application总数
String ServerCache = Cache.Count.ToString(); //应用程序缓存总数
// 应用程序占用内存
// String ServerFso = Check("Scripting.FileSystemObject"); // FSO 文本文件读写
String ServerTimeOut = Server.ScriptTimeout.ToString() + "毫秒"; // 本页执行时间
HTTP_USER_AGENT 获得用户使用的浏览器类型和版本
REMOTE_ADDR 获取用户的IP地址
REQUEST_METHOD 获取请求的方法
LOCAL_ADDR 获取服务器IP地址
SERVER_NAME 获取服务器主机名
PATH_INFO 获取当前执行程序的虚拟路径
PATH_TRANSLATED 获取当前执行程序的绝对路径
CONTENT_LENGTH 获取请求程序所发送内容的字符总数
CONTENT_TYPE 获取请求的信息类型
GATEWAY_INTERFACE 获取网关接口
QUERY_STRING 获取URL的附加信息
SCRIPT_NAME 获取当前程序的文件名(包含虚拟路径)
SERVER_PORT 获取服务器接受请求的端口
SERVER_PROTOCOL 获取服务器遵从的协议以及版本号
HTTP_ACCEPT_LANGUAGE 获取用户所使用的语言
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class AdminLogin_Default : System.Web.UI.Page
{
#region 必需的设计器变量
protected string MemberName, AdminLevel;
protected string ServerOS, CpuSum, CpuType, ServerSoft, MachineName, ServerName, ServerPath, ServerNet, ServerArea, ServerTimeOut, ServerStart;
protected string PrStart, AspNetN, AspNetCpu, ServerSessions, ServerApp, ServerCache, ServerAppN, ServerFso, RunTime;
#endregion
protected void Page_Load(object sender, EventArgs e)
{
DataLoad();
}
#region 获取服务器及用户信息
private void DataLoad()
{
DateTime sTime = DateTime.Now;
ServerOS = Environment.OSVersion.ToString(); //操作系统:
CpuSum = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS"); //CPU个数:
CpuType = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER"); //CPU类型:
ServerSoft = Request.ServerVariables["SERVER_SOFTWARE"]; //信息服务软件:
MachineName = Server.MachineName; //服务器名
ServerName = Request.ServerVariables["SERVER_NAME"]; //服务器域名
ServerPath = Request.ServerVariables["APPL_PHYSICAL_PATH"]; //虚拟服务绝对路径
ServerNet = ".NET CLR " + Environment.Version.ToString(); //DotNET 版本
ServerArea = (DateTime.Now - DateTime.UtcNow).TotalHours > 0 ? "+" + (DateTime.Now - DateTime.UtcNow).TotalHours.ToString() : (DateTime.Now - DateTime.UtcNow).TotalHours.ToString(); //服务器时区
ServerTimeOut = Server.ScriptTimeout.ToString(); //脚本超时时间
ServerStart = ((Double)System.Environment.TickCount / 3600000).ToString("N2"); //开机运行时长
PrStart = GetPrStart(); //进程开始时间
AspNetN = GetAspNetN(); //AspNet 内存占用
AspNetCpu = GetAspNetCpu(); //AspNet CPU时间
ServerSessions = Session.Contents.Count.ToString(); //Session总数
ServerApp = Application.Contents.Count.ToString(); //Application总数
ServerCache = Cache.Count.ToString(); //应用程序缓存总数
ServerAppN = GetServerAppN(); //应用程序占用内存
ServerFso = Check("Scripting.FileSystemObject"); //FSO 文本文件读写
ServerTimeOut = Server.ScriptTimeout.ToString() + "毫秒"; //本页执行时间
if (ServerSoft == "")
{
ServerSoft = "无";
}
#region 页面初始化必需的内容
Header.Title = "沉紫龙" + " - 系统管理";
//HeaderTextInfo.Initialization();
//HeaderTextInfo.GetAdminHeader(this, null);
#endregion
#region WebControls Config
// ViewState Config
this.EnableViewState = false;
#endregion
//执行时间
DateTime eTime = DateTime.Now;
RunTime = ((eTime - sTime).TotalMilliseconds).ToString();
}
#endregion
#region 获取服务器系统信息
private string GetServerAppN()
{
string temp;
try
{
temp = ((Double)GC.GetTotalMemory(false) / 1048576).ToString("N2") + "M";
}
catch
{
temp = "未知";
}
return temp;
}
private string GetAspNetN()
{
string temp;
try
{
temp = ((Double)System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + "M";
}
catch
{
temp = "未知";
}
return temp;
}
private string GetAspNetCpu()
{
string temp;
try
{
temp = ((TimeSpan)System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime).TotalSeconds.ToString("N0");
}
catch
{
temp = "未知";
}
return temp;
}
private string GetPrStart()
{
string temp;
try
{
temp = System.Diagnostics.Process.GetCurrentProcess().StartTime.ToString();
}
catch
{
temp = "未知";
}
return temp;
}
private string Check(string obj)
{
try
{
object claobj = Server.CreateObject(obj);
return "支持";
}
catch
{
return "不支持";
}
}
#endregion
}