-2

I am trying to recursively go through a directory and use GetFiles to return a list of all the files in that directory. Here is my code so far:

public string[] passFiles(string location)
        {
            string[] files;
            try
            {
                files = Directory.GetFiles(location);
                return files;
            }
            catch (UnauthorizedAccessException)
            {
                // Code here will be hit if access is denied.
                throw;
            }
        }

But it still gives me an Access Denied error. When I try to leave the catch part blank, it says that all paths must return something, so that's why I put the throw statement. Any ideas as to why this isn't ignoring the error and going on to the next one?

Alan
  • 265
  • 1
  • 3
  • 15
  • 2
    Where is the recursion here? – Steve Oct 30 '16 at 19:25
  • @Steve I imagine he's calling the function recursively rather than the function itself being recursive. – James Gould Oct 30 '16 at 19:26
  • @JayGould, then how come it's a recursive function at all? A Recursive Function is by definition a function which calls itself – Rahul Oct 30 '16 at 19:27
  • Well, if that is the case it is a waste of time given the fact that we have a Directory.GetFiles that could walk through the subfolders without explicit recursion code – Steve Oct 30 '16 at 19:28
  • Using Directory.GetFiles to read the content of subdirectories will not work if access to some of those subdirectories is denied. See [this question](https://stackoverflow.com/questions/1393178/unauthorizedaccessexception-cannot-resolve-directory-getfiles-failure) for other people having the same issue – NineBerry Oct 30 '16 at 19:33
  • @Rahul it's not my code, just trying to explain what I think OP might have meant. – James Gould Oct 30 '16 at 19:54

2 Answers2

2

When an exception occurs and you catch the exception, you still need to return a result for the function. Initialize your variable files to contain an empty array and then return it after the try-catch-block, so it is always returned, even when an error occurs.

public string[] passFiles(string location)
{
    // Create an empty array that will be returned in case something goes wrong
    string[] files = new string[0];
    try
    {
        files = Directory.GetFiles(location);
    }
    catch (UnauthorizedAccessException)
    {
        // Code here will be hit if access is denied.
    }

    return files;
}

See also this question for a similar question and some useful answers.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • What's the point in returning an empty array? doesn't serve any purpose. Better return null in that case. – Rahul Oct 30 '16 at 19:33
  • @Rahul See [this topic](http://stackoverflow.com/questions/1969993/is-it-better-to-return-null-or-empty-collection) on a discussion of your opinion. – NineBerry Oct 30 '16 at 19:38
  • Wow. Worked perfectly. Thank you so much! Another small question, now how do I modify this to catch an exception if the file is in use? Would it be something like `catch (IOException)`? – Alan Oct 30 '16 at 20:10
  • You can just write `catch ()` to catch any kind of exception. – NineBerry Oct 30 '16 at 20:12
0

You need to return something in the case where you catch the exception.

i.e. maybe return null at the end of the method - and remember to check for that in the calling code.

reddal
  • 353
  • 2
  • 8