PandaNL
PandaNL

Reputation: 848

C# Won't display size in subdirectorys

I call the following function to check a specific path and get a list from all the folders located in the given path, for each folder it will check what the folder size is.

This works if i have a file in the folder like this.

Givenfolderpath\Underlying_folder\file.txt

But when there is a subdirectory with a file in it, it will not give me the size of the folder.

Givenfolderpath\Underlying_folder\subdirectory\file.txt

This is my code.

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;
        }

Where does it go wrong?

It will put the data like this.

data in datagrid

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;
        }

Upvotes: 1

Views: 136

Answers (1)

Spontifixus
Spontifixus

Reputation: 6660

Try replacing the line

DirectoryInfo[] diArr = di.GetDirectories();

with

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

That should iterate over all directories contained in the given one. See for reference MSDN: DirectoryInfo.GetDirectories Method (String, SearchOption) and MSDN: SearchOption.

Upvotes: 1

Related Questions