在Asp.Net服务端开发的时候有需要需要获取很详细的服务器相关信息,服务器端口、服务器IIS版本、服务器用户名、项目所在服务器目录、服务器磁盘数量、Cpu、以及程序所占用内存等。


这些功能用过一次之后应该永久性保存起来,以备不时之需。


下面分享一个Asp.Net服务器探针小DEMO,助你更简单的获取服务器信息

ASP.Net探针获得系统属性方法大全

ASP.Net探针获得系统属性方法大全


HTML部分:此处没重点,都是服务器控件,仅做参考,重点在.cs文件中,请继续向下看


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ServerInfo._Default" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Asp.net探针 www.jsons.cn</title>
    <style type="text/css">
BODY{font-size:12px;color:#000000}
TABLE{font-size:12px;color:#000000}
TD{font-size:12px;color:#000000}
FORM{padding-right:0px;padding-left:0px;padding-bottom:0px;margin:0px;padding-top:0px}
TD TD{padding-left:12px}
A{color:#000090}
A:hover{color:#ff3333}
.InputTxt{border-right:#333333 1px solid;border-top:#333333 1px solid;font-size:12px;border-left:#333333 1px solid;width:120px;border-bottom:#333333 1px solid;height:18px}
.Button{border-right:#333333 1px solid;border-top:#333333 1px solid;font-size:12px;background:#ffffff;border-left:#333333 1px solid;width:32px;border-bottom:#333333 1px solid;height:16px}
.TrHead{font-weight:600;font-size:12px;background:#339900;color:#ffffff;height:28px;text-align:center}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%" align="center" border="0">
        <tr>
            <td style="font-weight: 600; font-size: 16px; color: #339900" align="center" height="30">
                <a href="http://www.jsons.cn" target="_blank">
                    <h1>
                         Asp.Net 探针</h1>
                </a>www.jsons.cn
            </td>
        </tr>
    </table>
    <table style="font-size: 12px; color: black; font-family: Verdana, Tahoma; border-collapse: collapse;
        background-color: whitesmoke" bordercolor="#666666" cellspacing="0" width="760"
        align="center" bgcolor="#eeeeee" border="1">
        <tr>
            <td align="center">
                <table style="border-collapse: collapse" bordercolor="#ffffff" cellspacing="0" width="95%"
                    align="center" border="1">
                    <tr>
                        <td class="TrHead" colspan="2" style="height: 21px">
                            服务器基本信息
                        </td>
                    </tr>
                    <tr>
                        <td width="38%" align="left" style="height: 29px">
                            服务器计算机名
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbServerName" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服务器IP地址
                        </td>
                        <td align="left">
                            <asp:Label ID="lbIp" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服务器域名
                        </td>
                        <td align="left">
                            <asp:Label ID="lbDomain" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="27" align="left" style="height: 27px">
                            服务器端口
                        </td>
                        <td align="left" style="height: 27px">
                            <asp:Label ID="lbPort" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服务器IIS版本
                        </td>
                        <td align="left">
                            <asp:Label ID="lbIISVer" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="24" align="left" style="height: 24px">
                            本文件所在文件夹
                        </td>
                        <td align="left" style="height: 24px">
                            <asp:Label ID="lbPhPath" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服务器操作系统
                        </td>
                        <td align="left">
                            <asp:Label ID="lbOperat" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            系统所在文件夹
                        </td>
                        <td align="left">
                            <asp:Label ID="lbSystemPath" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服务器脚本超时时间
                        </td>
                        <td align="left">
                            <asp:Label ID="lbTimeOut" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服务器的语言种类
                        </td>
                        <td align="left">
                            <asp:Label ID="lbLan" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="27" align="left" style="height: 27px">
                            .NET Framework 版本
                        </td>
                        <td align="left" style="height: 27px">
                            <asp:Label ID="lbAspnetVer" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" style="height: 29px">
                            服务器当前时间
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbCurrentTime" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服务器IE版本
                        </td>
                        <td align="left">
                            <asp:Label ID="lbIEVer" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服务器上次启动到现在已运行
                        </td>
                        <td align="left">
                            <asp:Label ID="lbServerLastStartToNow" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td width="38%" height="4" align="left" style="height: 4px">
                            逻辑驱动器
                        </td>
                        <td align="left" style="height: 4px">
                            <asp:Label ID="lbLogicDriver" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            CPU 总数
                        </td>
                        <td align="left">
                            <asp:Label ID="lbCpuNum" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            CPU 类型
                        </td>
                        <td align="left">
                            <asp:Label ID="lbCpuType" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" style="height: 29px">
                            虚拟内存
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbMemory" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" style="height: 29px">
                            当前程序占用内存
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbMemoryPro" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" style="height: 29px">
                            Asp.net所占内存
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbMemoryNet" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" style="height: 29px">
                            Asp.net所占CPU
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbCpuNet" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" style="height: 29px">
                            当前Session数量
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbSessionNum" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" style="height: 29px">
                            当前SessionID
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbSession" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" style="height: 29px">
                            当前系统用户名
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbUser" runat="server"></asp:Label>
                        </td>
                    </tr>
                </table>
                <br>
                <table width="95%" border="0">
                    <tr>
                        <td align="center" class="TrHead">
                            Asp.net探针 <a href="http://www.jsons.cn" target="_blank">www.jsons.cn</a>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
Asp.net服务器探针详细功能
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;
using System.Globalization;
using Microsoft.Win32;
using System.IO;
using System.Management;
using System.Diagnostics;

namespace ServerInfo
{
    /// <summary>
    /// Asp.net服务器探针
    /// www.jsons.cn
    /// </summary>
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /// <summary>
                /// 取应用程序路径
                /// </summary>
                lbServerName.Text = "http://"   HttpContext.Current.Request.Url.Host   HttpContext.Current.Request.ApplicationPath;
                lbIp.Text = Request.ServerVariables["LOCAl_ADDR"];
                lbDomain.Text = Request.ServerVariables["SERVER_NAME"].ToString();
                lbPort.Text = Request.ServerVariables["Server_Port"].ToString();
                lbIISVer.Text = Request.ServerVariables["Server_SoftWare"].ToString();
                lbPhPath.Text = Request.PhysicalApplicationPath;
                lbOperat.Text = Environment.OSVersion.ToString();
                lbSystemPath.Text = Environment.SystemDirectory.ToString();
                lbTimeOut.Text = (Server.ScriptTimeout / 1000).ToString()   "秒";
                lbLan.Text = CultureInfo.InstalledUICulture.EnglishName;
                lbAspnetVer.Text = string.Concat(new object[] { Environment.Version.Major, ".", Environment.Version.Minor, Environment.Version.Build, ".", Environment.Version.Revision });
                lbCurrentTime.Text = DateTime.Now.ToString();

                RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Version Vector");
                lbIEVer.Text = key.GetValue("IE", "未检测到").ToString();
                lbServerLastStartToNow.Text = ((Environment.TickCount / 0x3e8) / 60).ToString()   "分钟";
                string[] achDrives = Directory.GetLogicalDrives();
                for (int i = 0; i < Directory.GetLogicalDrives().Length - 1; i  )
                {
                    lbLogicDriver.Text = lbLogicDriver.Text   achDrives[i].ToString();
                }

                //ManagementClass diskClass = new ManagementClass("NUMBER_OF_PROCESSORS");
                lbCpuNum.Text = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS").ToString();
                lbCpuType.Text = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER").ToString();
                lbMemory.Text = (Environment.WorkingSet / 1024).ToString()   "M";
                lbMemoryPro.Text = ((Double)GC.GetTotalMemory(false) / 1048576).ToString("N2")   "M";
                lbMemoryNet.Text = ((Double)Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2")   "M";
                lbCpuNet.Text = ((TimeSpan)Process.GetCurrentProcess().TotalProcessorTime).TotalSeconds.ToString("N0");
                lbSessionNum.Text = Session.Contents.Count.ToString();
                lbSession.Text = Session.Contents.SessionID;
                lbUser.Text = Environment.UserName;
            }
        }
    }
}

代码都在此了,没什么可仔细说的,请下载DEMO查看


ASP.Net探针获得系统属性方法大全

原文链接:ASP.Net探针获得系统属性方法大全