0

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..

Gokul E
  • 1,356
  • 2
  • 13
  • 28
  • Seconded, an example of the methodology you are using would really help. Also, under what profile is VS running? What are the permissions on that account? – CodeBeard Aug 13 '13 at 19:41
  • `FastDirectoryEnumerator` is not part of the .net framework. The execption is happening inside there. If it is your code, you need to show that too. If it is someone else's code, you need to ask them for help. – Scott Chamberlain Aug 13 '13 at 19:48
  • 1
    http://stackoverflow.com/questions/8548012/unauthorizedaccessexception-while-getting-files – phillip Aug 13 '13 at 19:48
  • No no.. I Got it from CodeProject.Com [link](http://www.codeproject.com/Articles/38959/A-Faster-Directory-Enumerator) – Gokul E Aug 13 '13 at 19:49
  • `but even though Exception` What exception? In what code? – tnw Aug 13 '13 at 19:49
  • `UnAuthorizedAccessException` While Searching Through `SystemVolumeInformation` Directory of `D:\` – Gokul E Aug 13 '13 at 19:50
  • @phillip `Directory.EnumerateDirectories` This is from .NET 4.0 but am Using .NET 3.5 only.. – Gokul E Aug 13 '13 at 19:53
  • Why not just catch the exception and continue along? – tnw Aug 13 '13 at 19:53
  • In the example @phillip gives, you will see it catches the `UnAuthorizedAccessException` thrown exception and ignores it to 'keep going'. You would have to break out of your fluent linq pattern to be able to catch those exceptions I think. – CodeBeard Aug 13 '13 at 19:54
  • @ScottChamberlain am searching simply like `FastDirectoryEnumerator.EnumerateFiles("D:\\","*.reg.zip",SearchOption.AllDirectories);` So here The SystemVolumeInformation Directory is Included by the .NET by Default.. How can i Prevent Searching such Direcotories???? – Gokul E Aug 13 '13 at 19:55
  • @Gokul is the increased speed of `FastDirectoryEnumerator` important to you? – CodeBeard Aug 13 '13 at 19:55
  • @CodeBeard Without Linq.??? or .. May be I can edit `FastDirectoryEnumerator` Class .. but I Need Help to Go Through.. AM Sorry for my level... – Gokul E Aug 13 '13 at 19:56
  • @CodeBeard Yes sir, Because I have got more than 125,324 files like that.. so i to get count .. a bit faster.. – Gokul E Aug 13 '13 at 19:57
  • @Gokul I may be wrong, but you are using the `FastDirectoryEnumerator` to enumerate the files, the exception is being thrown 'somewhere' as you work through the filing system. I am not familiar with this library, but you would have to trap that exception and discard it without leaving the enumeration. – CodeBeard Aug 13 '13 at 19:59
  • Yes sir.. I have to.. @CodeBeard.. – Gokul E Aug 13 '13 at 20:01

1 Answers1

0

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.

Community
  • 1
  • 1
CodeBeard
  • 515
  • 3
  • 10