在ListView中获取目录大小 [英] Get Directory Size In ListView
本文介绍了在ListView中获取目录大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我有一个带有2个列(文件夹路径和大小)的listview.在列表视图中,我希望能够在目录列中输入文件夹路径,然后确定目录和子目录的大小...
如果有人能在这方面提供帮助,那么…….我一直建议代码可以工作.....但是我该如何将其应用于列表视图...我将不需要下一步.
Hi
I have a listview with 2 colums (Folder Path & Size). In the listview i want to be able to enter in a folder path in the directory colum and then determine the size of the directory and sub directorys...
If anyone is able to assist in this much would be appericated....i have been suggest code that works....but how do i apply that to a listview... i no a for next will be need.
Function GetFolderSize(ByVal DirPath As String, _
Optional IncludeSubFolders as Boolean = True) As Long
Dim lngDirSize As Long
Dim objFileInfo As FileInfo
Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath)
Dim objSubFolder As DirectoryInfo
Try
''add length of each file
For Each objFileInfo In objDir.GetFiles()
lngDirSize += objFileInfo.Length
Next
''call recursively to get sub folders
''if you don''t want this set optional
''parameter to false
If IncludeSubFolders then
For Each objSubFolder In objDir.GetDirectories()
lngDirSize += GetFolderSize(objSubFolder.FullName)
Next
End if
Catch Ex As Exception
End Try
Return lngDirSize
End Function
推荐答案
好吧,您只需要使用DirectoryInfo对象的GetDirectories方法来获取当前目录中所有目录的数组.您只需将其添加到列表视图中,即可为数组中的每个目录调用GetFolderSize,如下所示:-
Well, you just need to use the GetDirectories method of the DirectoryInfo object to get an array of all directories in the current directory. The you can just add to the listview calling GetFolderSize for each directory in the array, something like this:-
Public Sub LoadDirectoriesInListView(ByVal currentDirectoryValue As String)
ListView1.Items.Clear()
currentDirectory = currentDirectoryValue
Dim currentDirectoryInfo As DirectoryInfo = New DirectoryInfo(currentDirectory)
Dim directoryArray As DirectoryInfo() = currentDirectoryInfo.GetDirectories()
Dim dir As DirectoryInfo
For Each dir In directoryArray
Dim lvi As New ListViewItem(dir.Name)
lvi.SubItems.Add(GetFolderSize(dir.FullName, False))
ListView1.Items.Add(lvi)
Next
End Sub
然后,您可以这样称呼它:-
Then you just call it like this:-
LoadDirectoriesInListView(currentDirectory)
希望对您有帮助
Hope this helps
这篇关于在ListView中获取目录大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文