在Asp.Net服務(wù)端開(kāi)發(fā)的時(shí)候有需要需要獲取很詳細(xì)的服務(wù)器相關(guān)信息,服務(wù)器端口、服務(wù)器IIS版本、服務(wù)器用戶名、項(xiàng)目所在服務(wù)器目錄、服務(wù)器磁盤(pán)數(shù)量、Cpu、以及程序所占用內(nèi)存等。


這些功能用過(guò)一次之后應(yīng)該永久性保存起來(lái),以備不時(shí)之需。


下面分享一個(gè)Asp.Net服務(wù)器探針小DEMO,助你更簡(jiǎn)單的獲取服務(wù)器信息

ASP.Net探針獲得系統(tǒng)屬性方法大全

ASP.Net探針獲得系統(tǒng)屬性方法大全


HTML部分:此處沒(méi)重點(diǎn),都是服務(wù)器控件,僅做參考,重點(diǎn)在.cs文件中,請(qǐng)繼續(xù)向下看


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

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Asp.net探針 hnxxbl.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://hnxxbl.cn" target="_blank">
                    <h1>
                         Asp.Net 探針</h1>
                </a>hnxxbl.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">
                            服務(wù)器基本信息
                        </td>
                    </tr>
                    <tr>
                        <td width="38%" align="left" style="height: 29px">
                            服務(wù)器計(jì)算機(jī)名
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbServerName" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服務(wù)器IP地址
                        </td>
                        <td align="left">
                            <asp:Label ID="lbIp" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服務(wù)器域名
                        </td>
                        <td align="left">
                            <asp:Label ID="lbDomain" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="27" align="left" style="height: 27px">
                            服務(wù)器端口
                        </td>
                        <td align="left" style="height: 27px">
                            <asp:Label ID="lbPort" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服務(wù)器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">
                            服務(wù)器操作系統(tǒng)
                        </td>
                        <td align="left">
                            <asp:Label ID="lbOperat" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            系統(tǒng)所在文件夾
                        </td>
                        <td align="left">
                            <asp:Label ID="lbSystemPath" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服務(wù)器腳本超時(shí)時(shí)間
                        </td>
                        <td align="left">
                            <asp:Label ID="lbTimeOut" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服務(wù)器的語(yǔ)言種類(lèi)
                        </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">
                            服務(wù)器當(dāng)前時(shí)間
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbCurrentTime" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服務(wù)器IE版本
                        </td>
                        <td align="left">
                            <asp:Label ID="lbIEVer" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            服務(wù)器上次啟動(dòng)到現(xiàn)在已運(yùn)行
                        </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">
                            邏輯驅(qū)動(dòng)器
                        </td>
                        <td align="left" style="height: 4px">
                            <asp:Label ID="lbLogicDriver" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            CPU 總數(shù)
                        </td>
                        <td align="left">
                            <asp:Label ID="lbCpuNum" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td height="26" align="left">
                            CPU 類(lèi)型
                        </td>
                        <td align="left">
                            <asp:Label ID="lbCpuType" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" style="height: 29px">
                            虛擬內(nèi)存
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbMemory" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" style="height: 29px">
                            當(dāng)前程序占用內(nèi)存
                        </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所占內(nèi)存
                        </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">
                            當(dāng)前Session數(shù)量
                        </td>
                        <td align="left" style="height: 29px">
                            <asp:Label ID="lbSessionNum" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" style="height: 29px">
                            當(dāng)前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">
                            當(dāng)前系統(tǒng)用戶名
                        </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://hnxxbl.cn" target="_blank">hnxxbl.cn</a>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
Asp.net服務(wù)器探針詳細(xì)功能
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服務(wù)器探針
    /// hnxxbl.cn
    /// </summary>
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /// <summary>
                /// 取應(yīng)用程序路徑
                /// </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", "未檢測(cè)到").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;
            }
        }
    }
}

代碼都在此了,沒(méi)什么可仔細(xì)說(shuō)的,請(qǐng)下載DEMO查看


ASP.Net探針獲得系統(tǒng)屬性方法大全

原文鏈接:ASP.Net探針獲得系統(tǒng)屬性方法大全