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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:49:51+00:00 2026-05-31T04:49:51+00:00

I am using DirectoryInfo and FileInfo in .Net 4.0 to enumerate files in a

  • 0

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 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-31T04:49:52+00:00Added an answer on May 31, 2026 at 4:49 am

    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.

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

Sidebar

Related Questions

I have two programs which are accessing a directory using the .NET Directory class
I have a controller that returns a list of files in a directory using
How can I read all jpeg files in a directory using a Filestream so
I have these two collections: List<string> files = Directory.GetFiles(FilePath).ToList().Select(file => new FileInfo(file).Name).ToList(); string[] dirs
Basically, I have this code: DirectoryInfo dir = new DirectoryInfo(@\\MYNETWORK11\ABCDEFG\ABCDEFGHIJKL\00806\); FileInfo[] files = dir.GetFiles(200810*);
I am populating a datagrid control using files in a specified path (DirectoryInfo). I
I am using C# to copy files from one directory to another directory. I
I am using enumeratefiles to list all the files in a directory. Its throws
I have an IEnumerable<DirectoryInfo> that I want to filter down using an array of
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.

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.