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 Stac
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.