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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:25:18+00:00 2026-06-09T04:25:18+00:00

I’m trying to locate a file in a subdirectory of a specified folder (Let’s

  • 0

I’m trying to locate a file in a subdirectory of a specified folder (Let’s call it MyFolder) and save the subdirectory to a string variable to be used elsewhere. The problem is, that there may be multiple subdirectories in that specified folder, and each subdirectory may contain subdirectories of their own. Only one of said subdirectories in the tree under the MyFolder has the file I’m looking for.

In the example below, it could be in either of (possibly more, but for this example) two locations, but NEVER both.

-Root    
--/MyFolder    
---/a    
-----/aa    
-------------/blah.txt  (or if not here, it may be...)    
---/b    
-------/blah.txt  (...right here, possibly)

I’m trying to locate blah.txt, of course.

I’ve made a recursive foreach loop as seen below, but it will only return the sub-directory if the file is found in the very first level of sub-directories (/a, or /b). If the file is in /aa or /bb or deeper (root/a/aa/aaa, for example), it returns NULL every time. I’ve spent hours upon hours trying to fix this by slowly stepping through the debugger and watching the locals window closely, but I can’t seem to figure out what’s wrong. It does find the file and stores the correct sub-directory path while in the second foreach loop, but then, for some reason, it starts looping backwards (showing the previous directories until it hits the return "Null";). This does not happen if the file is found within the first level of sub-directories, Only if it finds it within the second level or deeper.

class Program
{
    static string dir = @"C:\MyFolder\";

    static void Main(string[] args)
    {
        Console.WriteLine(GetDirectory(dir));
        Console.ReadKey();
    }

    static string GetDirectory(string dir)
    {
        foreach (string dName in Directory.GetDirectories(dir))
        {
            foreach (string fName in Directory.GetFiles(dName, "blah.txt"))
            {
                return dName;
            }
            GetDirectory(dName);
        }
        return "NULL";
    }
}

The only way I’ve seen anything like this work is if you’re not trying to save the path as a string able to be used elsewhere (possibly even by other functions), but to have it displayed immediately as a void return type, such as in the console (By changing the return to "Console.WriteLine(dName)" and modifying the function call appropriately).

I’m very new to C#, my only coding experience previously has been in JavaScript and AutoHotkey, so my knowledge on languages such as this is still extremely limited, and all I have is a C# beginners guide book I’ve read through and the Internet… and I’ve searched through both valiantly to no avail for this specific issue.. so as it were, any help or pointers are 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-06-09T04:25:19+00:00Added an answer on June 9, 2026 at 4:25 am

    Why no tuse the recursive search built in?

    Something like

    Directory.GetFiles(@"C:\MyFolder\", "blah.txt", SearchOption.AllDirectories)
    

    Have a look at Directory.GetFiles Method (String, String, SearchOption) and SearchOption Enumeration

    Or even DirectoryInfo.

    Something like

    new DirectoryInfo(@"C:\MyFolder\").GetFiles("blah.txt", SearchOption.AllDirectories)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.