Question

Hi all am working with Directories and Files Counting Software..

Here, When I get or search Files from C:\ or D:\ it throws UnAuthorizedAccessException

I want to Enumerate Files Ignoring the file/Directory which is Inaccessible

How to ?? C# Visual Studio 2008 Exactly .NET Framework 3.5 only.

My code

var files = FastDirectoryEnumerator.EnumerateFiles(path, "*.reg.zip", 
                 SearchOption.AllDirectories)
                .GroupBy(f => f.Name).Select(g => g.First());

Here Am Taking files which ends with .reg.zip

I want to search it on my Whole Computer.. But the Exception..

VS 2008 Default User.. I Tried app.manifest with

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

but even though Exception.. while Searching in SystemVolumeInformation Directory..

Was it helpful?

Solution

I will start by saying this is not the best advice. However, it is a place to start. My answer is two fold:

Firstly, I think you have prematurely optimised by using the FastDirectoryEnumerator before understanding the filing system level security issues you will face with your project. As such, if time permits, I would recommend a simpler solution as recommended in the post linked to by @phillip in the comments (UnauthorizedAccessException while getting files). For a remotely modern machine 120k files shouldn't be a problem.

Secondly, looking at the speed stats of the FastDirectoryEnumerator I can see the appeal of using it. Digging into the code a little, I can see that it doesn't protect you in any way from permissions exceptions when used as you have done. It is essentially procedural code wrapped inside an IEnumerable MoveNext method. The MoveNext method itself, has been made recursive for sub-directory processing.

I am not able to test this next bit, so you will have to experiment. If you want to hack around with the FastDirectoryEnumerator the first thing you might want to try is a try catch block around the creation of the new FileData object.

public FileData Current { get { return new FileData(m_path, m_win_find_data); } }

If that doesn't work, you would have to work through the GetNext() method to trap the exception in the right place. You might need to go as far as implementing a NullObject pattern for FileData. I hope that helps in some way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top