0

I am attempting(without much luck) to search for a specific file but only if it exist in a particular directory.

For example: Say I would like to search for test.txt but only if it resides within the directory structure of TestFolder(does not have to be in TestFolder but does have to be in one of the subfolders of TestFolder). The location of TestFolder is not known.

I have tried this, but it errors out when it hits a dir it does not have access too.

string[] list = Directory.GetDirectories("c://", "TestFolder", SearchOption.AllDirectories);

The though was to find the directory and then search within the directory for the file.

Any help would be greatly appreciated!

BanksySan
  • 27,362
  • 33
  • 117
  • 216
samuraiY
  • 35
  • 1
  • 2
  • 6
  • http://stackoverflow.com/questions/172544/ignore-folders-files-when-directory-getfiles-is-denied-access and http://stackoverflow.com/questions/1393178/unauthorizedaccessexception-cannot-resolve-directory-getfiles-failure are almost equally good explanations. – Alexei Levenkov Apr 19 '16 at 22:29

1 Answers1

1

The way you describe should work (i.e. find directories and then search within them).

Most likely you got "UnauthorizedAccessException" exception which is related to security system in Windows 7.

I would suggest you add a try-catch to skip the folder you can't access

Mostafa abdo
  • 616
  • 1
  • 8
  • 21