Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every one

I am trying to get information about files in specified Directory

so i am using this code
C#
string path = @"D:\Directory";
            string[] filepathes = Directory.GetFiles(path, ".", SearchOption.AllDirectories);
            var items = from f in filepathes
                        select new ListViewItem(
                            new string[] {
                            new FileInfo(f).Directory.ToString(),
                            new FileInfo(f).Name.ToString(),
                            new FileInfo(f).CreationTime.ToString(),
                            new FileInfo(f).Length.ToString()});


but I got error
"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."

I know this error happens when file path too long

but my question is there any way to avoid this error


thanks to all
Posted
Updated 18-Mar-18 21:59pm
Comments
[no name] 20-Apr-13 15:52pm    
Avoid the error? Sure, don't do path names that are too long. You can catch the error and ignore it....

That is a limitation of the API functions what the .net framework is calling. This does note mean, that Windows or .net can not handle such long file names. Just the built-in tools are limited. Shame, but that's it :(
There are libraries out there you can use. Like these:
http://zetalongpaths.codeplex.com/[^]
http://bcl.codeplex.com/wikipage?title=Long%20Path&referringTitle=Home[^]
http://gallery.technet.microsoft.com/DelimonWin32IO-Library-V40-7ff6b16c[^]
Just a note: they might be not absolutely complete. I suggest you look for more, try out as many as you can and find the most suitable for you.
 
Share this answer
 
The method of getting information about files whose filename is too long is to P/Invoke to the Windows API. I believe that's what the libraries which Zoltán has linked to will be doing under the hood (in fact the first one even links to some pages about it), so I won't add to that information here.

I assume the exception happens when you try to create the FileInfo and not when getting the list of files. In which case you already have the paths, so you can either trap the exception, or check the length of the path before attempting to construct the FileInfo, and do something else in that case - e.g. exclude the path from your list, or report that the path is too long, or try to get the required information through P/Invoke (or by using a library mentioned by Zoltán).

Given that you're trying to get all the information of files in the sub-directory, you should probably also do something if permissions are denied to you, etc, so I'd go with the trapping the exception. (Besides which, checking whether the file path exceeds the length restrictions is more complicated than simply checking its length, since both the path and the filename length that matters. Such checks will just slow down the loop).

Additionally, you are constructing FileInfo for each file four times. I believe each instance will cache many of the properties of the file on construction, so this will be unnecessarily slow. (See the note in the Examples of FileInfo[^])

With all this in mind, regardless of the path too long problem, I'd seriously consider modifying this code.

I'm afraid I don't know anything about "select" syntax that you've used, so if I was to rework this I'd probably do something like this:

C#
string path = @"D:\Directory";
string[] filePaths = Directory.GetFiles(path, ".", SearchOption.AllDirectories);
foreach (string f in filePaths)
{
  try
  {
    FileInfo info = new FileInfo(f);
    ListViewItem item = new ListViewItem(new string[] {
      info.DirectoryName,         // Note use of this instead of Directory.ToString() !!
      info.Name.ToString(),
      info.CreationTime.ToString(),
      info.Length.ToString()});
    // Do what you need to do with "item"
  }
  catch(PathTooLongException)
  {
    // Ignore, or add listview item to report error, or
    // do something to try to get the information
    // (e.g. P/Invoke to the Windows API).
    // Note since you already have the full path name, you could use
    // System.IO.Path to get the directory and file name.
    // E.g.
    ListViewItem item = new ListViewItem(new string[] {
      Path.GetDirectoryName(f),
      Path.GetFileName(f),
      "Path too long",   /* Error message in "Created Time" field.  This is only an example, you probably don't want to do this in real application! Maybe have an additional "error" field? */
      ""});
    // Do what you need to do with "item"
  }
  catch(SecurityException)
  {
    // Ignore, or add listview item reporting permission denied, as above.
  }
  catch(UnauthorizedAccessException)
  {
    // Ignore, or add listview item reporting permission denied, as above.
  }
}

Regards,
Ian.
 
Share this answer
 
Comments
Jake Black Smith 9-Aug-21 5:23am    
You can try LongPath Tool Program that is best for solving long path files errors
thanks a lot to all

like Zoltán Zörgő said I used

Delimon.Win32.IO.dll library instead of system.IO and It Worked Fine

the Final Code Is :

C#
string path = @"D:\books";
            string[] filepathes = System.IO.Directory.GetFiles(path, ".", System.IO.SearchOption.AllDirectories);
var items = from f in filepathes
select new ListViewItem(
new string[] {
new Delimon.Win32.IO.FileInfo(f).Directory.FullName, // Directory
new Delimon.Win32.IO.FileInfo(f).Name.ToString(), // Name
new Delimon.Win32.IO.FileInfo(f).CreationTime.ToString(),
new Delimon.Win32.IO.FileInfo(f).Length.ToString()});
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900