Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:20:36+00:00 2026-05-27T23:20:36+00:00

Hi all i have been trying to search a specified directory and all sub

  • 0

Hi all i have been trying to search a specified directory and all sub directories for all files that have the specified file extension. However the inbuilt command is useless as it errors up and dies if you dont have access to a directory. So here’s what i have at the moment:

 Private Function dirSearch(ByVal path As String, Optional ByVal searchpattern As String = ".exe") As String()
    Dim di As New DirectoryInfo(path)
    Dim fi As FileInfo
    Dim filelist() As String
    Dim i As Integer = 0
    For Each fi In di.GetFiles
        If System.IO.Path.GetExtension(fi.FullName).ToLower = searchpattern Then
            filelist(i) = fi.FullName
            i += 1
        End If
    Next
    Return filelist
 End Function

However i get an “System.NullReferenceException: Object reference not set to an instance of an object.” when i try to access the data stored inside the filelist string array.

Any idea’s on what im doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:20:37+00:00Added an answer on May 27, 2026 at 11:20 pm

    You didn’t instantiate the Dim filelist() As String array. Try di.GetFiles(searchPattern)

    Dim files() as FileInfo = di.GetFiles(searchPattern)
    

    Use static method Directory.GetFiles that returns an array string

    Dim files =  Directory.GetFiles(Path,searchPattern,searchOption)
    

    Demo:

     Dim files() As String
     files = Directory.GetFiles(path, "*.exe", SearchOption.TopDirectoryOnly)
     For Each FileName As String In files
         Console.WriteLine(FileName)
     Next
    

    Recursive directory traversal:

       Sub Main()
            Dim path = "c:\jam"
            Dim fileList As New List(Of String)
    
            GetAllAccessibleFiles(path, fileList)
    
            'Convert List<T> to string array if you want
            Dim files As String() = fileList.ToArray
    
            For Each s As String In fileList
                Console.WriteLine(s)
            Next
        End Sub
    
        Sub GetAllAccessibleFiles(path As String, filelist As List(Of String))
            For Each file As String In Directory.GetFiles(path, "*.*")
                filelist.Add(file)
            Next
            For Each dir As String In Directory.GetDirectories(path)
                Try
                    GetAllAccessibleFiles(dir, filelist)
                Catch ex As Exception
    
                End Try
            Next
        End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to search answer for this, but all discussions that I
I have been trying to test my application to make sure that all the
I have been trying to do a search of the user directory and subdirectories
All i have something i have been trying to do for a while and
I have been trying all afternoon to get the jQuery Sifr Plugin ( http://jquery.thewikies.com/sifr/
I have been trying all day to get some data properly formatted in a
I have been trying to install ReviewBoard and all looks like it has gone
I have been trying to compute the total sum of all these ord s
Alright so I have been trying to get all of my separate classes (At
Hey all, i have been trying for a few minutes on this problem and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.