using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Management.Instrumentation;
using System.Management;
using System.Runtime.InteropServices;
using System.Web.Configuration;
using System.Configuration;
using Microsoft.Win32;
using System.Web.Hosting;
using System.Net;
namespace Song.ViewData
{
///
/// 服务器信息
///
public sealed class Server
{
private static readonly Server _instance = new Server();
private Server() { }
public static Server GetServer()
{
return _instance;
}
private string _ip = string.Empty;
///
/// 服务器IP
///
public string IP
{
get {
if (string.IsNullOrWhiteSpace(_ip))
_ip = WeiSha.Common.Server.IP;
return _ip;
}
}
///
/// 是否是本机IP
///
public bool IsLocalIP
{
get
{
string ip = this.IP;
if (ip == "127.0.0.1") return true;
if (this.Domain.ToLower().Trim() == "localhost") return true;
return false;
}
}
///
/// 是否是内网IP
///
public bool IsIntranetIP
{
get
{
if (IsLocalIP) return true;
string ip = this.IP;
if (ip.Substring(0, 3) == "10." || ip.Substring(0, 7) == "192.168" || ip.Substring(0, 7) == "172.16.")
{
return true;
}
return false;
}
}
///
/// 服务器访问端口
///
public string Port
{
get {
string port = "80";
try
{
if (HttpContext.Current != null)
port = System.Web.HttpContext.Current.Request.Url.Port.ToString();
if (port == "443") port = "80";
return port;
}
catch
{
return port;
}
}
}
///
/// 站点的访问域名
///
///
public string Domain
{
get
{
try
{
if (HttpContext.Current != null)
return System.Web.HttpContext.Current.Request.Url.Host.ToString();
}
catch
{
return "";
}
return "";
}
}
///
/// db.config中配置的根域
///
public string RootDomain
{
get
{
try
{
return WeiSha.Common.Server.RootDomain;
}
catch
{
return "";
}
return "";
}
}
///
/// 站点的访问域名带端口,如:http://www.xx.com/
///
///
public string DomainPath
{
get
{
string path = string.Empty;
if (HttpContext.Current != null)
{
path = "http://" + Domain + ":" + Port;
}
return path;
}
}
private string _os = string.Empty;
///
/// 服务器操作系统
///
public string OS
{
get
{
if (string.IsNullOrWhiteSpace(_os))
_os = WeiSha.Common.Server.OS;
return _os;
}
}
///
/// IIS版本
///
public string IISVersion
{
get
{
//RegistryKey表示 Win注册表中的项级节点.此类是注册表封装
string issversion = string.Empty;
RegistryKey getkey = Registry.LocalMachine.OpenSubKey("software\\microsoft\\inetstp");
if (getkey != null)
issversion = System.Convert.ToInt32(getkey.GetValue("majorversion", -1)).ToString();
return issversion;
}
}
///
/// CPU个数
///
public int CPUCount
{
get
{
try
{
string count = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS"); //CPU个数:
return System.Convert.ToInt16(count);
}
catch
{
return 0;
}
}
}
private string _CpuHz = string.Empty;
///
/// CPU主频,单位 GHz
///
public string CPUHz
{
get
{
if (string.IsNullOrWhiteSpace(_CpuHz))
_CpuHz = WeiSha.Common.Server.CPUHz;
return _CpuHz;
}
}
private double _ramSize = -1;
///
/// 物理内存大小
///
public double RamSize
{
get
{
if (_ramSize < 0) _ramSize = WeiSha.Common.Server.RamSize;
return _ramSize;
}
}
///
/// .Net FramwWork版本号
///
public string DotNetVersion
{
get
{
string netver = Environment.Version.ToString(); //DotNET 版本
if (netver.IndexOf('.') < 0) return netver;
netver = netver.Substring(0, 3);
return netver;
}
}
private string _cpu_id = string.Empty;
///
/// 获取CPU的序列号,由于某些原因,可能获取不到
///
public string CPU_ID
{
get
{
if (string.IsNullOrWhiteSpace(_cpu_id))
_cpu_id = WeiSha.Common.Server.CPU_ID;
return _cpu_id;
}
}
///
/// 当前应用程序的物理路径
///
public string ProgramPath
{
get
{
return System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
}
}
public DateTime _initDate = DateTime.Now.AddYears(-200);
///
/// 系统部署运行的初始时间
///
public DateTime InitDate
{
get
{
if (_initDate > DateTime.Now.AddYears(-100)) return _initDate;
_initDate = WeiSha.Common.License.Value.InitDate;
return _initDate;
}
}
}
}