C#Won';t在子目录中显示大小

C#Won';t在子目录中显示大小,c#,winforms,directory,C#,Winforms,Directory,我调用下面的函数来检查一个特定路径,并从给定路径中的所有文件夹中获取一个列表,对于每个文件夹,它将检查文件夹大小 如果我在这样的文件夹中有一个文件,这是可行的 static long GetDirectorySize(string p) { // 1 // Get array of all file names. string[] a = Directory.GetFiles(p, "*.*");

我调用下面的函数来检查一个特定路径,并从给定路径中的所有文件夹中获取一个列表,对于每个文件夹,它将检查文件夹大小

如果我在这样的文件夹中有一个文件,这是可行的

static long GetDirectorySize(string p)
        {
            // 1
            // Get array of all file names.
            string[] a = Directory.GetFiles(p, "*.*");
            // 2
            // Calculate total bytes of all files in a loop.
            long b = 0;
            foreach (string name in a)
            {
                // 3
                // Use FileInfo to get length of each file.
                FileInfo info = new FileInfo(name);
                b += info.Length;
            }
            // 4
            // Return total size
            return b;
        }
Givenfolderpath\Underground\u folder\file.txt

但是当有一个子目录包含一个文件时,它不会给出文件夹的大小

给定文件夹路径\基础文件夹\子目录\文件.txt

这是我的密码

public void ListFolders()
    {
        dataGridView1.Rows.Clear();
        DirectoryInfo di = new DirectoryInfo(Properties.Settings.Default.RevitPath);
        DirectoryInfo[] diArr = di.GetDirectories();

        foreach (DirectoryInfo dri in diArr)
        {
            string strCreateTime = dri.LastWriteTime.ToString();
            string strCreateDate = dri.LastWriteTime.ToString();
            string strCreateSize2 = null;
            string strCreateSizeMb = null;
            int strCreateSize3;

            long strCreateSize1 = GetDirectorySize(Properties.Settings.Default.RevitPath + @"\" + dri);
            string strCreateSize = GetSizeReadable(strCreateSize1);

            strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf(" "));
            strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf(" "));
            strCreateSize2 = strCreateSize.Remove(strCreateSize.LastIndexOf(" "));
            strCreateSizeMb = strCreateSize.Remove(0, strCreateSize.LastIndexOf(" "));
            strCreateSize3 = Convert.ToInt32(strCreateSize2);


            if (strCreateSizeMb == " Mb")
            {
                if (strCreateSize3 >= Properties.Settings.Default.FolderSize)
                {
                    notifyIcon1.ShowBalloonTip(20000, "Attention Required!", dri + " exceed the permissible size " + Properties.Settings.Default.FolderSize + " Mb", System.Windows.Forms.ToolTipIcon.Warning);
                }
            }

            int idx = dataGridView1.Rows.Add();
            DataGridViewRow row = dataGridView1.Rows[idx];
            row.Cells["User"].Value = dri;
            row.Cells["Date"].Value = strCreateTime;
            row.Cells["Time"].Value = strCreateDate;
            row.Cells["Size"].Value = strCreateSize2;
            row.Cells["SizeMB"].Value = strCreateSizeMb;
        }
哪里出了问题

它将把数据放在这样的位置

static long GetDirectorySize(string p)
        {
            // 1
            // Get array of all file names.
            string[] a = Directory.GetFiles(p, "*.*");
            // 2
            // Calculate total bytes of all files in a loop.
            long b = 0;
            foreach (string name in a)
            {
                // 3
                // Use FileInfo to get length of each file.
                FileInfo info = new FileInfo(name);
                b += info.Length;
            }
            // 4
            // Return total size
            return b;
        }

试着换一条线

DirectoryInfo[] diArr = di.GetDirectories();

它应该遍历给定目录中包含的所有目录。请参阅以获取参考。

尝试更换线路

DirectoryInfo[] diArr = di.GetDirectories();


它应该遍历给定目录中包含的所有目录。请参阅以获取参考和。

除了读入子目录并汇总其文件大小之外,您别无选择。看,除了读入子目录并总结其文件大小之外,您没有其他方法。查看请编辑问题并添加
GetDirectorySize()
-method的代码谢谢,当您参考GetDirectorySize()时,我选中了它并添加了SearchOption.AllDirectories。这解决了我的问题。请编辑问题并添加
GetDirectorySize()
-方法的代码谢谢,当您参考GetDirectorySize()时,我选中了它并添加了SearchOption.AllDirectory。这解决了我的问题。