fluentsharp / FluentSharp

Fluent API for the .NET Framework (used by the O2 Platform)
63 stars 18 forks source link

getListOfAllFilesFromDirectory should handle file access denied #9

Open DinisCruz opened 10 years ago

DinisCruz commented 10 years ago

in https://github.com/o2platform/FluentSharp/blob/master/FluentSharp.CoreLib/O2_DotNetWrappers/Windows/Files.cs#L274

At the moment if there is an access denied in one of the files or folders, there will be no results returned

The expectation is that the files that are currently available to the user should be returned, and that any errors should be logged

DinisCruz commented 10 years ago

Actually this is not that easy to fix since the current search is done using the .NET GetFiles methods which is the one that fails when one file cannot be accessed

var searchOptions = (bSearchRecursively) ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
String[] sFileMatches = Directory.GetFiles(sStartDirectory, sSearchPattern, searchOptions);
nacht commented 9 years ago

Try Directory.Enumerate instead of GetFiles

DinisCruz commented 9 years ago

That should work

From http://msdn.microsoft.com/en-us/library/dd997370(v=vs.110).aspx

            foreach (var fi in diTop.EnumerateFiles())
            {
                try
                {
                    // Display each file over 10 MB; 
                    if (fi.Length > 10000000)
                    {
                        Console.WriteLine("{0}\t\t{1}", fi.FullName, fi.Length.ToString("N0"));
                    }
                }
                catch (UnauthorizedAccessException UnAuthTop)
                {
                    Console.WriteLine("{0}", UnAuthTop.Message);
                }
            }

it looks like diTop.EnumerateFiles() is not the one that throws the exception

@nacht wanna try to give this a go and send a PR :)

nacht commented 9 years ago

let me see if i can get a mono stack running locally :)

DinisCruz commented 9 years ago

take a look at http://blog.diniscruz.com/search/label/OSx

that might help