Bali C
Bali C

Reputation: 31241

Enumerating Files Throwing Exception

I am trying to enumerate through files on my computer using the below code but everytime it hits a file or dir that I don't have permission to read it throws an exception. Is there any way I can continue searching after the exception has been thrown? I know some people have had similar issues but is there any other way of doing this other than checking every file/folder individually?

try
{
    string[] files = Directory.GetFiles(@"C:\", "*.*",SearchOption.AllDirectories);
    foreach (string file in files)
    {
        Console.WriteLine(file);
    }
}
catch
{
}

Thanks for any help as this is driving me mad!

Upvotes: 8

Views: 2099

Answers (2)

Simon Mourier
Simon Mourier

Reputation: 139065

Here is some utility code based on Windows API that enumerates file system entries without throwing/catching exceptions.

Sample usage:

...
foreach (var fi in Utilities.EnumerateFileSystemEntries(@"c:\windows\system32", DirectoryEnumerateOptions.Recursive))
{
    Console.WriteLine(fi.FullName);
}
...

Utility classes:

[Flags]
public enum DirectoryEnumerateOptions
{
    None = 0x0,
    Recursive = 0x1,
    ThrowErrors = 0x2, // if you really want it
    ExpandEnvironmentVariables = 0x4,
}

public static class Utilities
{
    public static IEnumerable<FileSystemInfo> EnumerateFileSystemEntries(string directoryPath, DirectoryEnumerateOptions options = DirectoryEnumerateOptions.None)
    {
        if (directoryPath == null)
            throw new ArgumentNullException(nameof(directoryPath));

        if (!Path.IsPathRooted(directoryPath))
        {
            directoryPath = Path.GetFullPath(directoryPath);
        }

        return EnumerateFileSystemEntriesPrivate(directoryPath, options);
    }

    private static IEnumerable<FileSystemInfo> EnumerateFileSystemEntriesPrivate(string directoryPath, DirectoryEnumerateOptions options = DirectoryEnumerateOptions.None)
    {
        if (!Directory.Exists(directoryPath))
            yield break;

        var findPath = Normalize(directoryPath, options.HasFlag(DirectoryEnumerateOptions.ExpandEnvironmentVariables));
        if (!findPath.EndsWith("*"))
        {
            findPath = Path.Combine(findPath, "*");
        }

        var h = FindFirstFile(findPath, out var data);
        if (h == INVALID_HANDLE_VALUE)
        {
            if (options.HasFlag(DirectoryEnumerateOptions.ThrowErrors))
                throw new Win32Exception(Marshal.GetLastWin32Error());

            yield break;
        }

        if (Include(ref data))
        {
            yield return ToInfo(ref data, directoryPath);
            if (options.HasFlag(DirectoryEnumerateOptions.Recursive) && data.fileAttributes.HasFlag(FileAttributes.Directory))
            {
                foreach (var wfd in EnumerateFileSystemEntriesPrivate(Path.Combine(directoryPath, data.cFileName), options))
                {
                    yield return wfd;
                }
            }
        }
        do
        {
            if (!FindNextFile(h, out data))
            {
                if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_FILES)
                {
                    FindClose(h);
                    break;
                }
                continue;
            }

            if (Include(ref data))
            {
                yield return ToInfo(ref data, directoryPath);
                if (options.HasFlag(DirectoryEnumerateOptions.Recursive) && data.fileAttributes.HasFlag(FileAttributes.Directory))
                {
                    foreach (var wfd in EnumerateFileSystemEntriesPrivate(Path.Combine(directoryPath, data.cFileName), options))
                    {
                        yield return wfd;
                    }
                }
            }
        }
        while (true);
    }

    private static bool Include(ref WIN32_FIND_DATA data) => data.cFileName != "." && data.cFileName != "..";

    private static FileSystemInfo ToInfo(ref WIN32_FIND_DATA data, string directoryPath)
    {
        if (data.fileAttributes.HasFlag(FileAttributes.Directory))
            return new FileInfo(Path.Combine(directoryPath, data.cFileName));

        return new DirectoryInfo(Path.Combine(directoryPath, data.cFileName));
    }

    private static string Normalize(string path, bool expandEnvironmentVariables)
    {
        if (path == null)
            return null;

        string expanded;
        if (expandEnvironmentVariables)
        {
            expanded = Environment.ExpandEnvironmentVariables(path);
        }
        else
        {
            expanded = path;
        }

        if (expanded.StartsWith(_prefix))
            return expanded;

        if (expanded.StartsWith(@"\\"))
            return _uncPrefix + expanded.Substring(2);

        return _prefix + expanded;
    }

    [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern IntPtr FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData);

    [DllImport("kernel32", SetLastError = true)]
    private static extern bool FindClose(IntPtr hFindFile);

    [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA lpFindFileData);

    #pragma warning disable IDE1006 // Naming Styles
    private const int ERROR_NO_MORE_FILES = 18;
    private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
    #pragma warning restore IDE1006 // Naming Styles

    private const string _prefix = @"\\?\";
    private const string _uncPrefix = _prefix + @"UNC\";

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal struct WIN32_FIND_DATA
    {
        public FileAttributes fileAttributes;
        public uint ftCreationTimeLow;
        public uint ftCreationTimeHigh;
        public uint ftLastAccessTimeLow;
        public uint ftLastAccessTimeHigh;
        public uint ftLastWriteTimeLow;
        public uint ftLastWriteTimeHigh;
        public uint fileSizeHigh;
        public uint fileSizeLow;
        public uint dwReserved0;
        public uint dwReserved1;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string cFileName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
        public string cAlternateFileName;
    }
}

Upvotes: 0

CodesInChaos
CodesInChaos

Reputation: 108830

I came across the same problem just today. I hacked together the following code. If you want to use it in a real product you might need to improve the error handling. Since this was for a one-shot script I didn't care much.

static IEnumerable<string> EnumerateFilesRecursive(string root,string pattern="*")
{
    var todo = new Queue<string>();
    todo.Enqueue(root);
    while (todo.Count > 0)
    {
        string dir = todo.Dequeue();
        string[] subdirs = new string[0];
        string[] files = new string[0];
        try
        {
            subdirs = Directory.GetDirectories(dir);
            files = Directory.GetFiles(dir, pattern);
        }
        catch (IOException)
        {
        }
        catch (System.UnauthorizedAccessException)
        {
        }

        foreach (string subdir in subdirs)
        {
            todo.Enqueue(subdir);
        }
        foreach (string filename in files)
        {
            yield return filename;
        }
    }
}

To use it you can either:

string[] files = EnumerateFilesRecursive(@"C:\").ToArray();//Note the ToArray()
foreach (string file in files)
{
   Console.WriteLine(file);
}

which first enumerates all files, stores all file names in memory and only then displays them. Alternatively you can:

IEnumerable<string> files = EnumerateFilesRecursive(@"C:\");//Note that there is NO ToArray()
foreach (string file in files)
{
   Console.WriteLine(file);
}

Which writes while enumerating and thus doesn't need to keep all filenames in memory at the same time.

Upvotes: 11

Related Questions