4

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
David Gard
  • 11,225
  • 36
  • 115
  • 227
  • have a look at this http://stackoverflow.com/a/2242573/1954916 – Belial09 Apr 07 '13 at 19:32
  • 1
    @Belial09: How does that link differ from what OP has already tried? – Tim Schmelter Apr 07 '13 at 19:36
  • 1
    Have you looked to see what files the GetFiles enumeration is returning? – Mark Hall Apr 07 '13 at 19:38
  • @Tim Schmelter if you have a look at the other answers on the post, there is an LINQ solution. The Answer on the link differs because there are parameters entered to the GetFiles() function. – Belial09 Apr 07 '13 at 19:45
  • @Belial09: However, he wants the total file count in one directory, so i don't see why using a search-pattern or search-option could help. `dir.GetFiles().Count()` should work. – Tim Schmelter Apr 07 '13 at 19:49
  • Are you running this app as an administrator or with some less privileged user account? – Steve Apr 07 '13 at 19:50
  • @MarkHall - How do I do that? All of my efforts end in an error. – David Gard Apr 07 '13 at 20:02
  • @Steve - I'm a admin, running the program in debug mode through Visual Studio 2012 Express. However, I'm not sure how not running as an admin would result in more than the actual number of files being counted?! – David Gard Apr 07 '13 at 20:03
  • @Belial09 - I found that thread before posting. Sadly, it is of no help in thi scase. Thanks. – David Gard Apr 07 '13 at 20:04
  • @MarkHall - Nevermind, I figured out how to list the files. – David Gard Apr 07 '13 at 20:11
  • @Belial09 - I take it back. Directory.GetFiles("directory", "*.mp3").count worked. Thanks. – David Gard Apr 07 '13 at 20:17

3 Answers3

13

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

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.

David Gard
  • 11,225
  • 36
  • 115
  • 227
1

Check you do not have hidden files in tested directories. I check your code on my PC and it is working good.

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
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