Click here to Skip to main content
15,917,795 members
Articles / Programming Languages / C#
Tip/Trick

Dealing with PathTooLongException While Scanning Files With Long Paths in C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
25 Jun 2015CPOL 12.5K   9   2
This tip will help you in fixing the PathTooLongException and getting the full path of files giving this exception.

Introduction

Suppose you have a list of objects of class ‘FileInfo’. This class provides properties and instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.

C#
List<FileInfo> AllFilesInfo;

Let's say you have a DataTable 'dt' with columns ‘Name’ and ‘Path’. For all the files in AllFilesInfo object, you want to fill in the name and directory name in dataTable.

C#
foreach (FileInfo fileInfo in AllFilesInfo)
{
       DataRow dr = dt.NewRow();
       dr[0] = fileInfo.Name;
       dr[1] = fileInfo.DirectoryName;
}

The above code works well if you don’t have any file with long path. But as soon as your code finds any such file, you will encounter the below exception:

PathTooLongException  was caught: The specified path, file name, or both are too long.
The fully qualified file name must be less than 260 characters, and the directory name must be 
less than 248 characters.

When you debug and add a watch for fileInfo object, you will find both Directory and DirectoryName are throwing this exception. Now if you expand the ‘Non-public members’ and then the ‘base’ object of ‘fileInfo’, you will see a property named ‘FullPath’ giving the fullpath, which is what you need.

Click to enlarge image

FullPath’ is a protected property of class ‘FileSystemInfo’, which is a base class for both ‘FileInfo’ and ‘DirectoryInfo’. You can access the path as follows:

C#
DataRow dr = dt.NewRow();
dr[0] = fileInfo.Name;
try
{
    dr[1] = fileInfo.DirectoryName;
}
catch (PathTooLongException ex)
{
    FieldInfo fld = typeof(FileSystemInfo).GetField(
                                                    "FullPath",
                                                     BindingFlags.Instance |
                                                     BindingFlags.NonPublic);
    string fullPath = fld.GetValue(fileInfo).ToString();
    fullPath = fullPath.Remove(fullPath.IndexOf(fileInfo.Name)); // Removing file name from the fullpath
    dr[1] = fullPath;
}

By using the above code, you can access the fullPath of files with long names.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
For more technical articles please visit my blog at-
http://komalmangal.blogspot.in

Comments and Discussions

 
QuestionUseful Pin
Member 1111981628-Jun-15 14:32
Member 1111981628-Jun-15 14:32 
AnswerRe: Useful Pin
Komal Mangal28-Jun-15 20:26
Komal Mangal28-Jun-15 20:26 
Thank you Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.