3

I am using DirectoryInfo and FileInfo in .Net 4.0 to enumerate files in a directory tree and I am hitting PathTooLongException. Simplified version is below

public static class Test
{
    public static void Search(DirectoryInfo base)
    {
        foreach(var file in base.GetFiles())
        {
            try
            {
                Console.WriteLine(file.FullName);
            } catch(PathTooLongException ex)
            {
                // What path was this?
            }
        }
        foreach(var dir in base.GetDirectories())
        {
            Search(dir);
        }
    }
}

When the error is thrown, I want to know what file path caused the problem. Obviously I can't ask for FullName as that is what errored. I can get name from file.Name, but if I can't get the rest of the path as file.Directory gives a PathTooLongException even though the DirectoryInfo that the file was found from worked fine! (I can't use that though as the actual code is much more complex).

Looking through the stack trace it seems that it's using an internal path (I see a protected file.FullPath from debug), and trying to tear the directory from the full (oversized) path. Most of the problems seem to involve System.IO.Path.NormalizePath, with I hear went through a few changes in .Net 4.0. I have not tried on previous versions of the framework.

My questions:

  1. How can I get the full path from this exception; it is seemingly passed without any useful information.
  2. Why would the framework need to limit characters in a path to chop off the filename?

Thanks in advance for any help,
Andy

1 Answer 1

6

I can't think of any other way off the top of my head other than using reflection, or using a library or P/Invoke to use the Windows APIs that support long paths and manually check length. The full path is stored in a protected string field called FullPath

foreach(var dir in new DirectoryInfo (@"D:\longpaths")
                     .GetFileSystemInfos("*.*", SearchOption.AllDirectories))
{
    try
    {
        Console.WriteLine(dir.FullName);
    }
    catch (PathTooLongException)
    {
                FieldInfo fld = typeof(FileSystemInfo).GetField(
                                        "FullPath", 
                                         BindingFlags.Instance | 
                                         BindingFlags.NonPublic);
                Console.WriteLine(fld.GetValue(dir));  // outputs your long path
    }
}

If you're trying to actually do something with the files and not just check file length, I would suggest using a library like this one from the BCL team at Microsoft, however it doesn't create DirectoryInfos, FileInfo or FileSystemInfo, only strings. So it's might not be a drop-in replacement for your code it seems.

As for the answer to your second question, I recommend reading this blog post dealing with long paths in .NET. This is a quote from it that explains why they haven't been quick to add long path support in .NET.

Very few people complain about a 32K limit, so, problem solved? Not quite. There are several reasons we were reluctant to add long paths in the past, and why we’re still careful about it, related to security, inconsistent support in the Windows APIs of the \?\ syntax, and app compatibility.

It's a 3 part series and explains a few reasons why the API is the way it is and why the limitation is there.

3
  • Thanks Christopher, I feared this would be the case. It just seems silly to hold the path privately and not allow access to it without validating it first...even though it created the invalid path for me just fine. The LongPath library you pointed to looks great! Unfortunately I forgot to mention all the paths will be UNC so won't be able to use as is (yet). Will be very useful to look through the code though to see how to implement the direct API.
    – Andyrooger
    Mar 7, 2012 at 9:49
  • @Andyrooger, yeah, I forgot about that. I forked the code and added that in a while ago. You can get the code at its codeplex project. Use the source code, not the release, I fixed a silly bug. Mar 7, 2012 at 21:01
  • @ChristopherCurrens +1 for the FieldInfo tip!
    – Gayot Fow
    Jun 11, 2013 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.