1

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

如果我在这样的文件夹中有一个文件,这将有效。

Givenfolderpath\Underlying_folder\file.txt

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

Givenfolderpath\Underlying_folder\subdirectory\file.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;
        }
4

1 回答 1

1

换线试试

DirectoryInfo[] diArr = di.GetDirectories();

DirectoryInfo[] diArr = di.GetDirectories("*", SearchOption.AllDirectories);

这应该遍历给定目录中包含的所有目录。请参阅MSDN:DirectoryInfo.GetDirectories Method (String, SearchOption)MSDN:SearchOption以供参考。

于 2012-12-18T10:11:43.033 回答