using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TopWinCMS.BLL;
namespace TopWinCMS.WebUI.Admin.ControlPanel
{
    public partial class SystemSpace : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            RightsCheck.CheckWorkPower("O_4");
            
            BindList();
        }

        private void BindList()
        {
            DataTable dt_Directorys = new DataTable();
            DataRow dr_Directorys;

            dt_Directorys.Columns.Add(new DataColumn("Folder", System.Type.GetType("System.String")));
            dt_Directorys.Columns.Add(new DataColumn("Size", System.Type.GetType("System.String")));
            dt_Directorys.Columns.Add(new DataColumn("FileSize", System.Type.GetType("System.Int64")));

            DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/"));

            dr_Directorys = dt_Directorys.NewRow();
            dr_Directorys["Folder"] = @"\";

            long FileSize = DirSize(dirInfo.GetFiles());
            dr_Directorys["FileSize"] = FileSize;
            dr_Directorys["Size"] = dFileSize(FileSize);


            dt_Directorys.Rows.Add(dr_Directorys);


            foreach (DirectoryInfo di in dirInfo.GetDirectories())
            {
                dr_Directorys = dt_Directorys.NewRow();
                dr_Directorys["Folder"] = di.Name;

                FileSize = DirSize(di);
                dr_Directorys["FileSize"] = FileSize;
                dr_Directorys["Size"] = dFileSize(FileSize);

                dt_Directorys.Rows.Add(dr_Directorys);
            }

            dr_Directorys = dt_Directorys.NewRow();
            dr_Directorys["Folder"] = @"总计";
            FileSize = Convert.ToInt64(dt_Directorys.Compute("sum(FileSize)",""));
            dr_Directorys["Size"] = dFileSize(FileSize);
            dt_Directorys.Rows.Add(dr_Directorys);

            Repeater1.DataSource = dt_Directorys;
            Repeater1.DataBind();

        }
        private long DirSize(FileInfo[] fis)
        {
            long Size = 0;
            foreach (FileInfo fi in fis)
            {
                Size += fi.Length;
            }
            return Size;
        }

        private long DirSize(DirectoryInfo d)
        {
            long Size = 0;
            FileInfo[] fis = d.GetFiles();
            foreach (FileInfo fi in fis)
            {
                Size += fi.Length;
            }
            DirectoryInfo[] dis = d.GetDirectories();
            foreach (DirectoryInfo di in dis)
            {
                Size += DirSize(di);
            }
            return (Size);
        }
        private string dFileSize(double FileSize)
        {
            if (FileSize < 1024)
            {
                return FileSize + " B";
            }
            else if ((FileSize < 1024 * 1024))
            {
                return Math.Round((FileSize / 1024), 2) + " KB";
            }
            else
            {
                return Math.Round(FileSize / (1024 * 1024), 2) + " MB";
            }
        }
    }
}