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 7688919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:02:26+00:00 2026-05-31T20:02:26+00:00

I have the following code to recursively search for files through a directory, which

  • 0

I have the following code to recursively search for files through a directory, which returns a list of all xml files to me. All works well, except that xml files in the root directory are not included in the list.

I understand why, since the first thing it does is get the directories in the root, then get files, thus missing the GetFiles() call on the root. I tried including the GetFiles() call prior to the foreach, but the results are not as I expect.

public static ArrayList DirSearch(string sDir)
{
    try
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {
            foreach (string f in Directory.GetFiles(d, "*.xml"))
            {
                string extension = Path.GetExtension(f);
                if (extension != null && (extension.Equals(".xml")))
                {
                fileList.Add(f);
                }
            }
            DirSearch(d);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    return fileList;
}

My directory structure is something like:

RootDirectory
        test1.0.xml
            test1.1.xml
            test1.2.xml
  2ndLevDir
            test2.0.xml
            test2.1.xml
  3rdLevDir
               test3.0.xml
               test3.1.xml

Code returns:

test2.0.xml
test2.1.xml
test3.0.xml
test3.1.xml

I would like to return every file including:

test1.0.xml
test1.1.xml
test1.2.xml

Not very well verse with recursion. Any pointers would be greatly appreciated.

  • 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-31T20:02:27+00:00Added an answer on May 31, 2026 at 8:02 pm

    You could use this overload of Directory.GetFiles which searches subdirectories for you, for example:

    string[] files = Directory.GetFiles(sDir, "*.xml", SearchOption.AllDirectories);
    

    Only one extension can be searched for like that, but you could use something like:

    var extensions = new List<string> { ".txt", ".xml" };
    string[] files = Directory.GetFiles(sDir, "*.*", SearchOption.AllDirectories)
                        .Where(f => extensions.IndexOf(Path.GetExtension(f)) >= 0).ToArray();
    

    to select files with the required extensions (N.B. that is case-sensitive for the extension).


    In some cases it can be desirable to enumerate over the files with the Directory.EnumerateFiles Method:

    foreach(string f in Directory.EnumerateFiles(sDir, "*.xml", SearchOption.AllDirectories))
    {
        // do something
    }
    

    Consult the documentation for exceptions which can be thrown, such as UnauthorizedAccessException if the code is running under an account which does not have appropriate access permissions.

    If the UnauthorizedAccessException is a problem, then please see the fine answers at Directory.EnumerateFiles => UnauthorizedAccessException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code snippet. I'm trying to list all the files in
I have the following code which recursively operates on each element within a List
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code that does something very simple: visits recursively a tree
I have the following code which I'm using to get the user to input
I am using the following code for parsing of small xml files and it
I have a problem with the following code: public static void RestoreToolStripMenuItem(ToolStripMenuItem item, List<string>.Enumerator
I have the following code where I'm trying to get all the properties of
I have following code to divide one number recursively by another number: #include <iostream>
I have the following recursive code and it doesn't behave as expected (see details

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.