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:
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,
info.Name.ToString(),
info.CreationTime.ToString(),
info.Length.ToString()});
}
catch(PathTooLongException)
{
ListViewItem item = new ListViewItem(new string[] {
Path.GetDirectoryName(f),
Path.GetFileName(f),
"Path too long",
""});
}
catch(SecurityException)
{
}
catch(UnauthorizedAccessException)
{
}
}
Regards,
Ian.