ASP.NET获取服务器常用信息的方法——探针

服务器启动运行时间(单位:秒);服务器IP; 服务器域名;服务器操作系统;WEB服务器版本;.NET解释引擎版本;https支持情况;HTTP访问端口;服务器当前时间;虚拟目录绝对路径;虚拟目录绝对路径;虚拟目录Session总数;检查是否支持该组件..

服务器启动运行时间(单位:秒);服务器IP; 服务器域名;服务器操作系统;WEB服务器版本;.NET解释引擎版本;https支持情况;HTTP访问端口;服务器当前时间;虚拟目录绝对路径;虚拟目录绝对路径;虚拟目录Session总数;检查是否支持该组件等等信息:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web;
  5. using System.Runtime.InteropServices;
  6.  
  7.     /**//// <summary>
  8.     /// 服务器相关信息
  9.     /// </summary>
  10.     public static class ServerInfo
  11.     {
  12.         private static CPU_INFO CpuInfo;
  13.         private static MEMORY_INFO MemoryInfo;
  14.         /**//// <summary>
  15.         /// 静态构造函数
  16.         /// </summary>
  17.         static ServerInfo()
  18.         {
  19.             CpuInfo = new CPU_INFO();
  20.             GetSystemInfo(ref CpuInfo);
  21.  
  22.             MemoryInfo = new MEMORY_INFO();
  23.             GlobalMemoryStatus(ref MemoryInfo);
  24.         }
  25.         /**//// <summary>
  26.         /// 服务器启动运行时间(单位:秒)
  27.         /// </summary>
  28.         public static string RunTime
  29.         {
  30.             get { return Convert.ToString(Environment.TickCount / 0x3e8); }
  31.         }
  32.  
  33.         /**//// <summary>
  34.         /// 服务器IP
  35.         /// </summary>
  36.         public static string IP
  37.         {
  38.             get { return HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"]; }
  39.         }
  40.  
  41.         /**//// <summary>
  42.         /// 服务器域名
  43.         /// </summary>
  44.         public static string DomainName
  45.         {
  46.             get { return HttpContext.Current.Request.ServerVariables["SERVER_NAME"]; }
  47.         }
  48.  
  49.         /**//// <summary>
  50.         /// 服务器操作系统
  51.         /// </summary>
  52.         public static string OS
  53.         {
  54.             get { return Environment.OSVersion.ToString(); }
  55.         }
  56.  
  57.         /**//// <summary>
  58.         /// WEB服务器版本
  59.         /// </summary>
  60.         public static string Soft
  61.         {
  62.             get { return HttpContext.Current.Request.ServerVariables["SERVER_SOFTWARE"]; }
  63.         }
  64.  
  65.         /**//// <summary>
  66.         /// .NET解释引擎版本
  67.         /// </summary>
  68.         public static string Net
  69.         {
  70.             get { return ".NET CLR  " + Environment.Version.Major + "." + Environment.Version.Minor + "." + Environment.Version.Build + "." + Environment.Version.Revision; }
  71.         }
  72.  
  73.         /**//// <summary>
  74.         /// https支持情况
  75.         /// </summary>
  76.         public static string Https
  77.         {
  78.             get
  79.             {
  80.                 return HttpContext.Current.Request.ServerVariables["HTTPS"];
  81.             }
  82.         }
  83.  
  84.         /**//// <summary>
  85.         /// HTTP访问端口
  86.         /// </summary>
  87.         public static string Port
  88.         {
  89.             get { return HttpContext.Current.Request.ServerVariables["SERVER_PORT"]; }
  90.         }
  91.  
  92.         /**//// <summary>
  93.         /// 服务器当前时间
  94.         /// </summary>
  95.         public static string Time
  96.         {
  97.             get { return DateTime.Now.ToString(); }
  98.         }
  99.  
  100.         /**//// <summary>
  101.         /// 虚拟目录绝对路径
  102.         /// </summary>
  103.         public static string PPatch
  104.         {
  105.             get { return HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];  }
  106.         }
  107.  
  108.         /**//// <summary>
  109.         /// 虚拟目录绝对路径
  110.         /// </summary>
  111.         public static string NPatch
  112.         {
  113.             get { return HttpContext.Current.Request.ServerVariables["PATH_TRANSLATED"]; }
  114.         }
  115.  
  116.         /**//// <summary>
  117.         /// 虚拟目录Session总数
  118.         /// </summary>
  119.         public static string Session
  120.         {
  121.             get { return HttpContext.Current.Session.Count.ToString();  }
  122.         }
  123.  
  124.         /**//// <summary>
  125.         /// 虚拟目录Application总数
  126.         /// </summary>
  127.         public static string Application
  128.         {
  129.             get { return HttpContext.Current.Application.Count.ToString();}
  130.         }
  131.  
  132.         /**//// <summary>
  133.         /// 服务器计算机名
  134.         /// </summary>
  135.         public static string MachineName
  136.         {
  137.             get { return HttpContext.Current.Server.MachineName; }
  138.         }
  139.  
  140.         /**//// <summary>
  141.         /// 服务端脚本执行超时(单位:秒)
  142.         /// </summary>
  143.         public static string ScriptOut
  144.         {
  145.             get { return HttpContext.Current.Server.ScriptTimeout.ToString(); }
  146.         }
  147.  
  148.         服务器组件支持情况#region 服务器组件支持情况
  149.  
  150.         /**//// <summary>
  151.         /// 检查是否支持该组件
  152.         /// </summary>
  153.         /// <param name="sObject">组件名,ProgId或ClassId。</param>
  154.         /// <returns>是否支持该组件</returns>
  155.         public static bool Check(string sObject)
  156.         {
  157.             try
  158.             {
  159.                 //object chkObj = HttpContext.Current.Server.CreateObject(sObject);
  160.                 HttpContext.Current.Server.CreateObject(sObject);
  161.                 return true;
  162.             }
  163.             catch
  164.             {
  165.                 return false;
  166.             }
  167.         }
  168.  
  169.         /**//// <summary>
  170.         /// 是否支持Access
  171.         /// </summary>
  172.         public static bool Access
  173.         {
  174.             get { return Check("ADODB.RecordSet"); }
  175.         }
  176.  
  177.         /**//// <summary>
  178.         /// 是否支持FSO
  179.         /// </summary>
  180.         public static bool FSO
  181.         {
  182.             get { return Check("Scripting.FileSystemObject"); }
  183.         }
  184.  
  185.         /**//// <summary>
  186.         /// 是否支持CDONTS邮件发送
  187.         /// </summary>
  188.         public static bool CDONTSMail
  189.         {
  190.             get { return Check("CDONTS.NewMail"); }
  191.         }
  192.  
  193.         /**//// <summary>
  194.         /// 是否支持JMail邮件收发
  195.         /// </summary>
  196.         public static bool JMail
  197.         {
  198.             get { return Check("JMail.SmtpMail"); }
  199.         }
  200.  
  201.         /**//// <summary>
  202.         /// 是否支持ASPemail发信
  203.         /// </summary>
  204.         public static bool ASPemail
  205.         {
  206.             get { return Check("Persits.MailSender"); }
  207.         }
  208.  
  209.         /**//// <summary>
  210.         /// 是否支持Geocel发信
  211.         /// </summary>
  212.         public static bool Geoce
  213.         {
  214.             get { return Check("Geocel.Mailer"); }
  215.         }
  216.  
  217.         /**//// <summary>
  218.         /// 是否支持SmtpMail发信
  219.         /// </summary>
  220.         public static bool SmtpMail
  221.         {
  222.             get { return Check("SmtpMail.SmtpMail.1"); }
  223.         }
  224.  
  225.         /**//// <summary>
  226.         /// 是否支持ASPUpload文件上传
  227.         /// </summary>
  228.         public static bool ASPUpload
  229.         {
  230.             get { return Check("Persits.Upload.1"); }
  231.         }
  232.  
  233.         /**//// <summary>
  234.         /// 是否支持ASPCN文件上传
  235.         /// </summary>
  236.         public static bool ASPCNUpload
  237.         {
  238.             get { return Check("aspcn.Upload"); }
  239.         }
  240.  
  241.         /**//// <summary>
  242.         /// 是否支持SoftArtisans文件管理
  243.         /// </summary>
  244.         public static bool SoftArtisans
  245.         {
  246.             get { return Check("SoftArtisans.FileManager"); }
  247.         }
  248.  
  249.         /**//// <summary>
  250.         /// 是否支持Dimac文件上传
  251.         /// </summary>
  252.         public static bool DimacUpload
  253.         {
  254.             get { return Check("w3.upload"); }
  255.         }
  256.  
  257.         /**//// <summary>
  258.         /// 是否支持刘云峰文件上传组件
  259.         /// </summary>
  260.         public static bool LYFUpload
  261.         {
  262.             get { return Check("LyfUpload.UploadFile"); }
  263.         }
  264.  
  265.         /**//// <summary>
  266.         /// 是否支持Dimac的图像读写组件
  267.         /// </summary>
  268.         public static bool DimacImg
  269.         {
  270.             get { return Check("W3Image.Image"); }
  271.         }
  272.  
  273.     }
using System;using System.Collections.Generic;using System.Text;using System.Web;using System.Runtime.InteropServices;    /** <summary>    /// 服务器相关信息    /// </summary>    public static class ServerInfo    {        private static CPU_INFO CpuInfo;        private static MEMORY_INFO MemoryInfo;        /** <summary>        /// 静态构造函数        /// </summary>        static ServerInfo()        {            CpuInfo = new CPU_INFO();            GetSystemInfo(ref CpuInfo);            MemoryInfo = new MEMORY_INFO();            GlobalMemoryStatus(ref MemoryInfo);        }        /** <summary>        /// 服务器启动运行时间(单位:秒)        /// </summary>        public static string RunTime        {            get { return Convert.ToString(Environment.TickCount / 0x3e8); }        }        /** <summary>        /// 服务器IP        /// </summary>        public static string IP        {            get { return HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"]; }         }        /** <summary>        /// 服务器域名        /// </summary>        public static string DomainName        {            get { return HttpContext.Current.Request.ServerVariables["SERVER_NAME"]; }        }        /** <summary>        /// 服务器操作系统        /// </summary>        public static string OS        {            get { return Environment.OSVersion.ToString(); }        }        /** <summary>        /// WEB服务器版本        /// </summary>        public static string Soft        {            get { return HttpContext.Current.Request.ServerVariables["SERVER_SOFTWARE"]; }        }        /** <summary>        /// .NET解释引擎版本        /// </summary>        public static string Net        {            get { return ".NET CLR  " + Environment.Version.Major + "." + Environment.Version.Minor + "." + Environment.Version.Build + "." + Environment.Version.Revision; }        }        /** <summary>        /// https支持情况        /// </summary>        public static string Https        {            get            {                return HttpContext.Current.Request.ServerVariables["HTTPS"];            }        }        /** <summary>        /// HTTP访问端口        /// </summary>        public static string Port        {            get { return HttpContext.Current.Request.ServerVariables["SERVER_PORT"]; }        }        /** <summary>        /// 服务器当前时间        /// </summary>        public static string Time        {            get { return DateTime.Now.ToString(); }        }        /** <summary>        /// 虚拟目录绝对路径        /// </summary>        public static string PPatch        {            get { return HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];  }        }        /** <summary>        /// 虚拟目录绝对路径        /// </summary>        public static string NPatch        {            get { return HttpContext.Current.Request.ServerVariables["PATH_TRANSLATED"]; }        }        /** <summary>        /// 虚拟目录Session总数        /// </summary>        public static string Session        {            get { return HttpContext.Current.Session.Count.ToString();  }        }        /** <summary>        /// 虚拟目录Application总数        /// </summary>        public static string Application        {            get { return HttpContext.Current.Application.Count.ToString();}        }        /** <summary>        /// 服务器计算机名        /// </summary>        public static string MachineName        {            get { return HttpContext.Current.Server.MachineName; }        }        /** <summary>        /// 服务端脚本执行超时(单位:秒)        /// </summary>        public static string ScriptOut        {            get { return HttpContext.Current.Server.ScriptTimeout.ToString(); }        }        服务器组件支持情况#region 服务器组件支持情况        /** <summary>        /// 检查是否支持该组件        /// </summary>        /// <param name="sObject">组件名,ProgId或ClassId。</param>        /// <returns>是否支持该组件</returns>        public static bool Check(string sObject)        {            try            {                //object chkObj = HttpContext.Current.Server.CreateObject(sObject);                HttpContext.Current.Server.CreateObject(sObject);                return true;            }            catch            {                return false;            }        }        /** <summary>        /// 是否支持Access        /// </summary>        public static bool Access        {            get { return Check("ADODB.RecordSet"); }        }        /** <summary>        /// 是否支持FSO        /// </summary>        public static bool FSO        {            get { return Check("Scripting.FileSystemObject"); }        }        /** <summary>        /// 是否支持CDONTS邮件发送        /// </summary>        public static bool CDONTSMail        {            get { return Check("CDONTS.NewMail"); }        }        /** <summary>        /// 是否支持JMail邮件收发        /// </summary>        public static bool JMail        {            get { return Check("JMail.SmtpMail"); }        }        /** <summary>        /// 是否支持ASPemail发信        /// </summary>        public static bool ASPemail        {            get { return Check("Persits.MailSender"); }        }        /** <summary>        /// 是否支持Geocel发信        /// </summary>        public static bool Geoce        {            get { return Check("Geocel.Mailer"); }        }        /** <summary>        /// 是否支持SmtpMail发信        /// </summary>        public static bool SmtpMail        {            get { return Check("SmtpMail.SmtpMail.1"); }        }        /** <summary>        /// 是否支持ASPUpload文件上传        /// </summary>        public static bool ASPUpload        {            get { return Check("Persits.Upload.1"); }        }        /** <summary>        /// 是否支持ASPCN文件上传        /// </summary>        public static bool ASPCNUpload        {            get { return Check("aspcn.Upload"); }        }        /** <summary>        /// 是否支持SoftArtisans文件管理        /// </summary>        public static bool SoftArtisans        {            get { return Check("SoftArtisans.FileManager"); }        }        /** <summary>        /// 是否支持Dimac文件上传        /// </summary>        public static bool DimacUpload        {            get { return Check("w3.upload"); }        }        /** <summary>        /// 是否支持刘云峰文件上传组件        /// </summary>        public static bool LYFUpload        {            get { return Check("LyfUpload.UploadFile"); }        }        /** <summary>        /// 是否支持Dimac的图像读写组件        /// </summary>        public static bool DimacImg        {            get { return Check("W3Image.Image"); }        }    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值