asp.net 4.5 exercise ~ test14-3 statistics directory size

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test14_3.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="输入目录"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="计算目录大小" OnClick="Button1_Click" /><br />
        <asp:Label ID="lblShow" runat="server" Text=""></asp:Label>
        <asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15">
            <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
            <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px" />
            <ParentNodeStyle Font-Bold="False" />
            <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px" VerticalPadding="0px" />
        </asp:TreeView>
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace test14_3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string path = TextBox1.Text;
            if (Directory.Exists(path))
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                TreeNode node = new TreeNode(path);
                lblShow.Text = "目录大小:" + DirSize(dir, node).ToString() + "Byte";
                TreeView1.Nodes.Clear();
                TreeView1.Nodes.Add(node);
            }
            else
            {
                lblShow.Text = "目录不存在";
            
            }
        }

        public static long DirSize(DirectoryInfo dir, TreeNode parent)
        {
            long size = 0;
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo f in files)
            {
                TreeNode node = new TreeNode();
                node.Value = "文件:" + f.Name + " (大小:" + f.Length
                   + "Byte  日期:" + f.CreationTime + ")";
                parent.ChildNodes.Add(node);
                size += f.Length;
            }
            //子目录
            DirectoryInfo[] childDirs = dir.GetDirectories();
            foreach (DirectoryInfo d in childDirs)
            {
                TreeNode nodeC = new TreeNode();
                nodeC.Value = d.Name;
                nodeC.Text = "文件夹:" + d.Name + " (日期:" + d.CreationTime + ")";
                parent.ChildNodes.Add(nodeC);
                size += DirSize(d, nodeC);
            }

            return size;
        }
    }
}

 

Guess you like

Origin blog.csdn.net/modern358/article/details/114736255