Hi,

I need to write a c# app that will iterate through multiple directories, sub-directories and return info on all files found.

When I tried this the other week I had problems when FileInfo and GetDirectories returned exceptions when a file/folder name length exceeded 248 characters.

Has anyone written a program that can get all file info and deal with exceptions efficiently? (I want to be able to work through subdirectories even if the parent has an exception).

I'll post my previous attempt later if that helps.

Thanks

I want to be able to work through subdirectories even if the parent has an exception

The exception occurs most likely because of the missing privileges. Assuming you're reading file system while inside Windows and you have admin rights you could take the ownership of some folders and files. Still there are a few folders you can't access even as an admin.

Another point is to handle symbolic links correctly to avoid infinite loops.

When I tried this the other week I had problems when FileInfo and GetDirectories returned exceptions when a file/folder name length exceeded 248 characters.

I haven't yet tried this code myself but I found an article with sample code how to deal long path and file names: Zeta Long Paths.

Here's a snippet (code skeleton to be precise) from my code that reads a directory's information

using System.IO;

DirectoryInfo DirInfo;
DirectoryInfo[] ChildFolders;

DirInfo = new DirectoryInfo("<foldername>");
if (DirInfo != null)
{
    try
       {
            ChildFolders = DirInfo.GetDirectories();
        }
    catch (System.UnauthorizedAccessException)
       {
           // Access denied to this folder
           continue;
       }
    foreach (DirectoryInfo di in ChildFolders)
    {
        if (di != null)
        {
            if ((di.Attributes & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
            {
                // Handle this directory's information
            }
            else
            {
                // JUNCTION POINT (SYM LINK)
            }
        }
    }

And here's another snippet that reads file information

DirectoryInfo DirInfo;
FileInfo[] ChildFiles;
    
try
{
    DirInfo = new DirectoryInfo("<foldername>");
}
catch (System.UnauthorizedAccessException)
{
    // Access denied to this folder
    continue;
}
if (DirInfo != null)
{
    try
    {
        ChildFiles = DirInfo.GetFiles();
    }
    catch (System.UnauthorizedAccessException)
    {
        // Access denied
        continue;
    }
    foreach (FileInfo fi in ChildFiles)
    {
        if (fi != null)
        {
            // Handle this file's information
        }
    }
}

The point is not to show what information you can get, but how I've handled the exceptions.

HTH

Thanks - I'll take a look...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.