locked
VB - My.Computer.FileSystem.GetFiles

    Question

  • The second option in GetFiles includes a search option. I cannot get this to work using either FileIO.SearchOption.SearchTopLevelOnly (The wording used in the code snippet that I took from Microsoft), or Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly. This returns a value of 2, but I do not know what this value means. Has anyone any sugestions?

    PS I have checked that there are files with the designated extension in the directory

    Friday, May 25, 2007 8:40 AM

Answers

  • You need to use "*" to give a wildcard:

      extension(0) = "*.txt"
      extension(1) = "*.dbf"
    Saturday, May 26, 2007 4:27 PM

All replies

  • Maybe you've got the wrong directory path or wildcard?  This works:

    Public Class Form1
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each name As String In My.Computer.FileSystem.GetFiles("c:\windows", FileIO.SearchOption.SearchTopLevelOnly, "*.*")
          Console.WriteLine(name)
        Next
      End Sub
    End Class

    Friday, May 25, 2007 8:10 PM
  • Thanks for your response. As I read your sugestion, it should return all of the files in that directory. I can achieve this satisfactorily using: -

    Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String)

    fileList = My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory)

     

    However, If I want files with specific extensions, and I try: -

     

    Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String)

    Dim extension() As String

    ReDim extension(1)

    extension(0) = ".txt"

    extension(1) = ".dbf"

    fileList = My.Computer.FileSystem.GetFiles( _

    My.Computer.FileSystem.CurrentDirectory, _

    Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, extension)

     

    Then I get nothing, although I know that there are files in the specified directory of theses types.

     

    G

     

    Saturday, May 26, 2007 4:13 PM
  • You need to use "*" to give a wildcard:

      extension(0) = "*.txt"
      extension(1) = "*.dbf"
    Saturday, May 26, 2007 4:27 PM
  • So you want a list of all txt and dbf file

     

    Code Snippet

    Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles("c:\", FileIO.SearchOption.SearchTopLevelOnly, "*.dbf", "*.txt")

     

     

     

    This seems to work for me.

    Saturday, May 26, 2007 4:29 PM
  • Thanks, that was great.
    Sunday, May 27, 2007 3:50 PM