如何正确计算文件夹中的文件数 [英] How to correctly count the number of files in a folder
问题描述
我创建一个文件夹列表,其中每个文件夹只需要几个属性,所以我使用下面的类。但是,无论文件夹是什么, FilesInFolder
属性总是比文件夹中的实际文件数多5个。
I am making a list of folders, where each folder needs only a few properties, so I'm using the Class below. However, no matter the folder, the FilesInFolder
property is alway 5 more than the actual number of files in the folder.
有人可以帮助我找出什么是错的吗?感谢。
Can someone please help me find out what is wrong? Thanks.
Public Class Single_Action_Folder
Public ReadOnly FullName As String = ""
Public ReadOnly Name As String = ""
Public ReadOnly FilesInFolder As Integer = 0
Public ReadOnly Exists As Boolean = False
'**
' Constructor
'*
Public Sub New(Optional dir As DirectoryInfo = Nothing)
' First check that a directory has been specified
If dir Is Nothing Then Exit Sub
' Populate the class properties
FullName = dir.FullName
Name = dir.Name
FilesInFolder = dir.GetFiles().Count
Exists = dir.Exists
End Sub
End Class
推荐答案
所以这里的问题是 FilesInFolder = dir.GetFiles()。计数
计数隐藏文件。即使我设置了Windows文件夹选项来显示隐藏的文件/文件夹,他们没有显示,因为他们是像相册艺术。以下行排序我的问题。
So the issue here is that FilesInFolder = dir.GetFiles().Count
was counting hidden files. Even though I've set Windows folder options to show hidden files/folders, they were not shown as they were things like album art. The following line sorted my issue.
FilesInFolder = Directory.GetFiles(FullName, "*.mp3").Count
我想知道,如果有一种方法来计算多个文件类型?例如MP3和WMA?如果有人知道,我会征求意见。
I am wondering though, if there is a way to count more than one file type? I.e MP3 and WMA? If anyone happens to know, I'd apprciate a comment.
这篇关于如何正确计算文件夹中的文件数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!