|
-
Dec 30th, 2019, 06:21 AM
#1
Re: How can I Get Sum Total Size of Directories Which Listed in ListBox?
I didn’t just get one directory size. I put all of the directory names from my documents folder in my listbox, then run the exact same code I gave you...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 30th, 2019, 06:50 AM
#2
Thread Starter
Lively Member
Re: How can I Get Sum Total Size of Directories Which Listed in ListBox?
 Originally Posted by .paul.
I didn’t just get one directory size. I put all of the directory names from my documents folder in my listbox, then run the exact same code I gave you...
I mean to say same. You put only Documents Folder. please put difrent folders (D:\XXX, or C:\Pictures and try again.)
Not try only with one folder inside listbox. Try your code with multiple folders...
-
Dec 30th, 2019, 10:15 AM
#3
Re: How can I Get Sum Total Size of Directories Which Listed in ListBox?
Code:
ListBox1.Items.AddRange(IO.Directory.GetDirectories("Z:\Documents"))
He did have multiple directories in the list.
The above line adds all the directories that are in Z:\Documents to the Listbox (note "AddRange" and "GetDirectories". GetDirectories returns an array of directory paths and AddRange adds all the elements of the array to the list.
The code then gets the size of each of the directories (and the subdirectories below those directories) and gives the sum.
That is what you said you wanted.
A test more similar to your posts.
Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add("C:\users\passel\Desktop\Tools") '7.5 MB
ListBox1.Items.Add("C:\users\passel\Desktop\DeskTop Collection 2") '691 MB
ListBox1.Items.Add("C:\users\passel\Desktop\DeskTopCollection") ' 121 MB
Dim dirSize As Long = ListBox1.Items.Cast(Of String).Sum(Function(s) (From strFile In My.Computer.FileSystem.GetFiles(s, FileIO.SearchOption.SearchAllSubDirectories) Select New IO.FileInfo(strFile).Length).Sum())
MsgBox((dirSize / 1024) / 1024) 'MB
End Sub
End Class
The message box shows the total 820.675497..... so matches the expected size of the three directories sizes (as noted in the comments).
I just removed one of the "/ 1024" so it is in MB rather than GB.
You said it is working, so I assume it is good for you. I guess you just assumed he was adding only one directory to the Listbox, rather than all the directories from the Z:\Documents folder.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Dec 31st, 2019, 01:55 AM
#4
Thread Starter
Lively Member
Re: How can I Get Sum Total Size of Directories Which Listed in ListBox?
 Originally Posted by passel
Code:
ListBox1.Items.AddRange(IO.Directory.GetDirectories("Z:\Documents"))
He did have multiple directories in the list.
The above line adds all the directories that are in Z:\Documents to the Listbox (note "AddRange" and "GetDirectories". GetDirectories returns an array of directory paths and AddRange adds all the elements of the array to the list.
The code then gets the size of each of the directories (and the subdirectories below those directories) and gives the sum.
That is what you said you wanted.
A test more similar to your posts.
Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add("C:\users\passel\Desktop\Tools") '7.5 MB
ListBox1.Items.Add("C:\users\passel\Desktop\DeskTop Collection 2") '691 MB
ListBox1.Items.Add("C:\users\passel\Desktop\DeskTopCollection") ' 121 MB
Dim dirSize As Long = ListBox1.Items.Cast(Of String).Sum(Function(s) (From strFile In My.Computer.FileSystem.GetFiles(s, FileIO.SearchOption.SearchAllSubDirectories) Select New IO.FileInfo(strFile).Length).Sum())
MsgBox((dirSize / 1024) / 1024) 'MB
End Sub
End Class
The message box shows the total 820.675497..... so matches the expected size of the three directories sizes (as noted in the comments).
I just removed one of the "/ 1024" so it is in MB rather than GB.
You said it is working, so I assume it is good for you. I guess you just assumed he was adding only one directory to the Listbox, rather than all the directories from the Z:\Documents folder.
Yes. its working. thanks a lot.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|