4

我正在制作一个文件夹列表,其中每个文件夹只需要几个属性,所以我使用下面的类。但是,无论是哪个文件夹,该FilesInFolder属性总是比文件夹中的实际文件数多 5 个。

有人可以帮我找出问题所在吗?谢谢。

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
4

3 回答 3

13

所以这里的问题FilesInFolder = dir.GetFiles().Count是计算隐藏文件。尽管我已将 Windows 文件夹选项设置为显示隐藏文件/文件夹,但它们并未显示,因为它们就像专辑封面一样。以下行对我的问题进行了排序。

FilesInFolder = Directory.GetFiles(FullName, "*.mp3").Count

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.

于 2013-04-07T20:20:10.393 回答
1

检查您在测试目录中没有隐藏文件。我在我的电脑上检查了你的代码,它运行良好。

于 2013-04-07T19:41:15.263 回答
0

use this code:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim FilesInFolder = Directory.GetFiles("C:\BGS\TemporaryProjects\Images\", "*.mp3").Count
    Dim i As Integer = 1
    While i <= FilesInFolder
        ListBox1.Items.Add(i)
        i += 1
    End While
End Sub
于 2020-03-09T12:56:15.407 回答