using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;
using System.Threading; 
using TopWinCMS.BLL; 
using TopWinCMS.Common;
namespace TopWinCMS.WebUI.Admin.ControlPanel
{
    /// <summary>
    /// Admin_System_Info 的摘要说明。
    /// </summary>
    public partial class ServerProbe : System.Web.UI.Page
    {

        [DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);

        //定义内存的信息结构
        [StructLayout(LayoutKind.Sequential)]
        public struct MEMORY_INFO
        {
            public uint dwLength;
            public uint dwMemoryLoad;
            public uint dwTotalPhys;
            public uint dwAvailPhys;
            public uint dwTotalPageFile;
            public uint dwAvailPageFile;
            public uint dwTotalVirtual;
            public uint dwAvailVirtual;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            //当Access数据库不存在的时候，用户名需要打开这个页面看看服务器的物理路径
            //Session["ComeIn"]这个Session只有在发生错误的时候生成
            if (Session["ComeIn"] == null)
            {
                
            }
           

            if (!(IsPostBack))
            {
                HttpServer myCommonClass = new HttpServer();
                TimeSpan stime = DateTime.Now.TimeOfDay;
                servername.Text = Server.MachineName;
                serverms.Text = Environment.OSVersion.ToString();
                serverip.Text = Request.ServerVariables["LOCAL_ADDR"];
                server_name.Text = Request.ServerVariables["http_host"];
                serversoft.Text = Request.ServerVariables["server_software"];
                servernet.Text = Environment.Version.Major + "." + Environment.Version.Minor + "." + Environment.Version.Build + "." + Environment.Version.Revision;
                serverhttps.Text = Request.ServerVariables["HTTPS"];
                serverport.Text = Request.ServerVariables["server_port"];
                serverout.Text = Server.ScriptTimeout.ToString();
                servertime.Text = DateTime.Now.ToString();
                serverarea.Text = (DateTime.Now - DateTime.UtcNow).TotalHours > 0 ? "+" + (DateTime.Now - DateTime.UtcNow).TotalHours.ToString() : (DateTime.Now - DateTime.UtcNow).TotalHours.ToString();
                LB_Cache.Text = HttpRuntime.Cache.Count.ToString();
                try
                {
                    aspnetn.Text = (System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + " MB";
                }
                catch
                {
                    aspnetn.Text = "系统拒绝提供。";
                }
                try
                {
                    aspnetcpu.Text = (System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime).TotalSeconds.ToString("N0") + " 秒";
                }
                catch
                {
                    aspnetcpu.Text = "系统拒绝提供。";
                }
                serverstart.Text = (System.Environment.TickCount / 3600000).ToString("N2");
                try
                {
                    prstart.Text = System.Diagnostics.Process.GetCurrentProcess().StartTime.ToString();
                }
                catch
                {
                    prstart.Text = "系统拒绝提供。";
                }
                cpuc.Text = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS");
                cputype.Text = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER");
                if (myCommonClass.CheckObject("ADODB.RecordSet") == true)
                {
                    serveraccess.Text = "支持";
                }
                else
                {
                    serveraccess.Text = "不支持";
                }
                if (myCommonClass.CheckObject("Scripting.FileSystemObject") == true)
                {
                    serverfso.Text = "支持";
                }
                else
                {
                    serverfso.Text = "不支持";
                }
                if (myCommonClass.CheckObject("JMail.SmtpMail") == true)
                {
                    jmail.Text = "支持";
                }
                else
                {
                    jmail.Text = "不支持";
                }

                //调用GlobalMemoryStatus函数获取内存的相关信息
                MEMORY_INFO MemInfo;
                MemInfo = new MEMORY_INFO();
                GlobalMemoryStatus(ref MemInfo);
                LbdwMemoryLoad.Text = MemInfo.dwMemoryLoad + " %";
                LbdwTotalPhys.Text =FileIO.FileSize(MemInfo.dwTotalPhys);
                LbdwAvailPhys.Text = FileIO.FileSize(MemInfo.dwAvailPhys);
                LbdwTotalPageFile.Text = FileIO.FileSize(MemInfo.dwTotalPageFile);
                LbdwAvailPageFile.Text = FileIO.FileSize(MemInfo.dwAvailPageFile);
                LbdwTotalVirtual.Text = FileIO.FileSize(MemInfo.dwTotalVirtual);


                serverppath.Text = Request.ServerVariables["APPL_PHYSICAL_PATH"];
                servernpath.Text = Request.ServerVariables["path_translated"];
                servers.Text = Session.Contents.Count.ToString();
                servera.Text = Application.Contents.Count.ToString();
                TimeSpan etime = DateTime.Now.TimeOfDay;
                TimeSpan t = etime.Subtract(stime);
                runtime.Text = t.TotalMilliseconds.ToString();

                if (myCommonClass.CheckObject("MSWC.AdRotator") == true)
                {
                    LB_AdRotator.Text = "支持";
                }
                else
                {
                    LB_AdRotator.Text = "不支持";
                }

                if (myCommonClass.CheckObject("MSWC.BrowserType") == true)
                {
                    LB_BrowserType.Text = "支持";
                }
                else
                {
                    LB_BrowserType.Text = "不支持";
                }
                if (myCommonClass.CheckObject("MSWC.NextLink") == true)
                {
                    LB_NextLink.Text = "支持";
                }
                else
                {
                    LB_NextLink.Text = "不支持";
                }

            }
        }

        protected void Testbtn_Click(object sender, System.EventArgs e)
        {
            TimeSpan t1 = DateTime.Now.TimeOfDay;

            for (int i = 0; i < 100000000; i++)
            {
                i += 1;
            }
            TimeSpan t2 = DateTime.Now.TimeOfDay;
            TimeSpan t3 = t2.Subtract(t1);
            showtest.Text = t3.TotalSeconds + " 秒";
        }

   

        protected void BT_Test_Click(object sender, EventArgs e)
        {
            HttpServer myCommonClass = new HttpServer();
            if (myCommonClass.CheckObject(TB_ObjName.Text.Trim()) == true)
            {

                MsgBox.Alert("支持", UpdatePanel1);
            }
            else
            {
                MsgBox.Alert("不支持", UpdatePanel1);
            }
        }


    }
}
