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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:45:38+00:00 2026-05-20T16:45:38+00:00

My application is C# 3.5 runs on Windows 7 Ultimate, 64 bit. It goes

  • 0

My application is C# 3.5 runs on Windows 7 Ultimate, 64 bit. It goes through all folder subfolders to perform its job. However, it fails (falls into the infinite loop until StackOverflow.com exception) if run against the folder which name is only one symbol which is #255.

To reproduce, you can do the following:

  1. run Windows Explorer create C:\Temp folder in this folder
  2. create new folder and rename it with Alt-255 (using numeric keypad)
  3. create subfolders “first” and “second” there
  4. create subfolders “1” and “2” under Temp

So you now have:

  • C:\1
  • C:\2
  • C:\ \first
  • C:\ \second

For such C:\Temp folder with a subfolder with the name #255 (or more #255 symbols) the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class Program
{
  public static string[] GetDirectories(string pathToTraverse)
  {
    List<string> result = new List<string>();

    foreach (DirectoryInfo subFolder in new DirectoryInfo(pathToTraverse).GetDirectories())
    {
      result.Add(subFolder.FullName);
    }
    return result.ToArray();
  }

  public static void TraverseFolders(string folderToTraverse)
  {
    foreach (string subFolder in GetDirectories(folderToTraverse))
    {
      Console.WriteLine(subFolder);

      TraverseFolders(subFolder);
    }
  }

  static void Main(string[] args)
  {
    TraverseFolders(@"C:\Temp");
  }
}

will never end and will give you result like:

C:\Temp\ 
C:\Temp\1
C:\Temp\2
C:\Temp\ 
C:\Temp\1
C:\Temp\2
C:\Temp\ 
C:\Temp\1
C:\Temp\2
C:\Temp\ 

So how do I correctly enumerate such folder subfolders?

  • 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-20T16:45:38+00:00Added an answer on May 20, 2026 at 4:45 pm

    The following program runs perfectly and does not result in a stack overflow error.

    using System;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string pathToTraverse = @"C:\Desktop";
                foreach (DirectoryInfo subFolder in new DirectoryInfo(pathToTraverse).GetDirectories())
                {
                    System.Console.WriteLine(subFolder);
                }
            }
        }
    }
    

    It produces the following output:

    chaff
    Python
    __history
     
    ÿ
    

    The penultimate apparently blank line is in fact the directory named Alt+255.

    Consequently I believe that your problem is not related to the code you have shown and is in fact elsewhere in some code that you have not presented to us.

    I’m running on Windows 7 with VS 2010 Express targeting .net 3.5.


    Now that your update shows all your code, I can see what is happening. The .net code is presumably trimming the directories and so the folders with white space get lost.

    So @"C:\Temp\ " is trimmed to @"C:\Temp\".

    I found the following trivial modification avoided the infinite loop:

    TraverseFolders(subFolder+@"\");
    

    Adding a trailing path separator stops the trimming that appears to occur in the call to DirectoryInfo. In the example above this means that @"C:\Temp\ \" is passed to DirectoryInfo which yields the expected results.

    I guess you should probably use a routine that only adds a trailing path separator if one is not already present. And you may want to avoid hardcoding the @”\” as path separator, but that’s for you to work out now that you know what the underlying cause of your problem is.

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

Sidebar

Related Questions

I have an application that runs on Windows Mobile 6 Professional PDA devices. The
I'm working on an application that runs on Windows Mobile 6 that needs to
Suppose I have a GUI-only application that runs on Windows and I'd like to
We are using c++ to develop an application that runs in Windows CE 4
I'm developing and application that runs as a Windows service. There are other components
I have a c# application that runs as a windows service controlling socket connections
I've written an application that runs flawlessly in windows, and throws this error on
I am trying to write a simple application that runs on a Windows Mobile
This is a strange one. A web application which runs fine on Windows Server
I have windows forms application wich runs another console application here is the part

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.