-1

I am new to C#. I am running an image gallery project and I want to get all .jpg files from the D:\ drive so I saw this code

string[] filePaths = Directory.GetFile(@"D:\", "*.jpg", SearchOption.AllDirectories);

But I get this error message of UnauthorizedAccessExeption Access to the path ... is denied.

Please is there a way I can search through all the sub folders of D:/ excluding the paths which dont have access permission. Because other users too wont have to edit the file permission

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
KANAYO AUGUSTIN UG
  • 2,078
  • 3
  • 17
  • 31
  • If the access to the path is denied, you probably need to give the user who executes the code the appropriate access to the path. – Uwe Keim Jan 12 '19 at 09:09
  • There are file access methods in C# IO; see [here](https://stackoverflow.com/questions/29226208/check-for-access-rights-on-a-folder-using-c-sharp) for an example; this is for write but could be adapted. Also reference to other answers with more information – Peter Smith Jan 12 '19 at 09:10

1 Answers1

0

As per Directory.GetFiles, the error UnauthorizedAccessException is caused by the following:

The caller does not have the required permission.

Also, I wouldnt run GetFiles with search option as AllDirectories but go through one directory at a time. I used this link:

UnauthorizedAccessException cannot resolve Directory.GetFiles failure

Answer 2 (not the accepted one)

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Gauravsa
  • 6,330
  • 2
  • 21
  • 30