如何在列表框列出的项目中获取总文件夹数? [英] How can I get total files folder count in listbox listed items ?
问题描述
表单中有一个ListBox然后
我在列表框中列出文件夹方式,其中包含FolderBrowserDialog;
C:\ Users \Yaman \Desktop\TestA'(有4个文件夹27个文件)
C:\ Users \Yaman \Desktop \ TestB' (有5个文件夹30个文件)
当我列出一个再见时,我可以获得文件夹和文件数。
如果我只列出这个;
C:\ Users \Yaman \Desktop \ TestA
结果是
27个文件/ 4个文件夹
但是当我列出两个文件夹方式时我可以得到只有最后列出的文件夹计数结果
如果我列出
C:\ Users \Yaman \\ \\Desktop \ TestA
C:\ Users \Yaman \Desktop \ TestA
结果最后列为此
30个文件/ 5个文件夹
我如何计算所有文件夹文件和子文件夹的数量一起列在列表框中?
我尝试过:
< pre lang =vb>< pre> Private Sub Button11_Click(发件人 As Object ,e As EventArgs) Handles Button11.Click
对于 z = 0 ListBox1。 Items.Count - 1
Dim TheSize As 长 = GetDirSize(ListBox1.Items.Item(z))' (C:\ Users \Yaman \Desktop \ Deneme)
Dim counterDir As 整数 = IO.Directory .GetDirectories(ListBox1.Items.Item(z), *。*,IO.SearchOption。 AllDirectories。.Length ' 计算文件数
Dim counterFile As Integer = IO.Directory.GetFiles(ListBox1.Items.Item (z), *。*,IO.SearchOption.AllDirectories).Count 计算文件数
Label5.Text = counterFile& Files& /& counterDir.ToString& 文件夹
下一页 z
结束 Sub
检查:
私人 Sub Button11_Click(发件人作为 对象,e As EventArgs)句柄 Button11.Click
Dim currpath As String =
Dim TheSize 作为 长 = 0
Dim counterFile 作为 整数 = 0
Dim counterDir As 整数 = 0
对于 z = 0 ListBox1.Items.Count - 1
currpath = ListBox1.Items.Item(z).ToString()
TheSize = GetDirSize(currpath)' (C:\ Users \Yaman \Desktop\Deneme)
counterDir + = IO.Directory.GetDirectories(currpath, *。*,IO.SearchOption.AllDirectories).Length 计算文件数
counterFile + = IO.Directory.GetFiles(currpath, *。*,IO.SearchOption.AllDirectories).Count ' 计算文件数
下一步 z
Label5.Text = counterFile& file(s)& span class =code-string> in& counterDir.ToString& 文件夹
结束 Sub
深入了解更改;)
有关详细信息,请参阅:范围Microsoft Docs [ ^ ]
There is an ListBox in the form then
I am listing Folders Way in Listbox With FolderBrowserDialog as this ;
C:\Users\Yaman\Desktop\TestA '(Has 4 Folders 27 Files)
C:\Users\Yaman\Desktop\TestB '(Has 5 Folders 30 Files)
I can get the folders and files count when I listed one bye one.
if I listed only this ;
C:\Users\Yaman\Desktop\TestA
in listbox the result is
27 Files / 4 Folders
but when I listed both folder way I can get only last listed folders count result
if I listed
C:\Users\Yaman\Desktop\TestA
C:\Users\Yaman\Desktop\TestA
then result is last listed as this
30 Files / 5 Folders
How can I get all folders files and subfolders count which together listed in listbox ?
What I have tried:
<pre>Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
For z = 0 To ListBox1.Items.Count - 1
Dim TheSize As Long = GetDirSize(ListBox1.Items.Item(z)) '("C:\Users\Yaman\Desktop\Deneme")
Dim counterDir As Integer = IO.Directory.GetDirectories(ListBox1.Items.Item(z), "*.*", IO.SearchOption.AllDirectories).Length 'counts the number of files
Dim counterFile As Integer = IO.Directory.GetFiles(ListBox1.Items.Item(z), "*.*", IO.SearchOption.AllDirectories).Count 'counts the number of files
Label5.Text = counterFile & " Files" & " / " & counterDir.ToString & " Folders"
Next z
End Sub
Check this:
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click Dim currpath As String = "" Dim TheSize As Long = 0 Dim counterFile As Integer = 0 Dim counterDir As Integer = 0 For z = 0 To ListBox1.Items.Count - 1 currpath = ListBox1.Items.Item(z).ToString() TheSize = GetDirSize(currpath) '("C:\Users\Yaman\Desktop\Deneme") counterDir += IO.Directory.GetDirectories(currpath, "*.*", IO.SearchOption.AllDirectories).Length 'counts the number of files counterFile += IO.Directory.GetFiles(currpath, "*.*", IO.SearchOption.AllDirectories).Count 'counts the number of files Next z Label5.Text = counterFile & " file(s)" & " in " & counterDir.ToString & " folder(s)" End Sub
Take a deep look at the changes ;)
For further details, please see: Scope in Visual Basic | Microsoft Docs[^]
这篇关于如何在列表框列出的项目中获取总文件夹数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!