Manually getting all files from directory

Arokh

Centurion
Joined
Apr 11, 2006
Messages
124
Hi,
surprisingly I couldn't find a topic which covers this (or I'm simply blind):

I switched over from VB to C# and I've been using the
My.Computer.FileSystem.GetFiles([SomeDir],FileIO.SearchOption.SearchAllSubDirectories,new String() {"*.avi","*.mkv"})
Method.
Since the My namespace is not available from C# I was trying to use the
System.IO.Directory.GetFiles() Method.
But the filter doesn't allow me to pick more than one file extension.

And another issue which is bugging me some time now,
is that the two methods are fairly unreliable.
As soon as the method encounters an access violation it stops and returns an empty array.
Which means as soon as an user decides to mess with the access permissions
of the folder I want to read the method might fail.

Not satisfied with that I wanted to write my own method,
but the only way I know how to enumerate through the entries in a folder is the Dir() function,
which is apparently also not available in C# (or at least it shouldn't be used anymore).

Speed is also of importance since the possibility of adding a folder which has some thousand files in its folder tree exists.

Is there another function which can enumerate though folder entries,
or is there a better way to do this?
 
Would something like
C#:
        private List<string> GetFiles(string path, params string[] patterns)
        {
            List<string> files = new List<string>();
            foreach (string pattern in patterns)
            {
                files.AddRange(Directory.GetFiles(path, pattern));
            }
            return files;
        }
not do the trick?

Not sure what you mean about an access violation returning an empty array though - do you mean a folder containing a file a user doesn't have permission to, the user not having permission to the folder itself or something else?
 
I did it the way you described for now.

What I meant about the access violation:
A very simple example would be
object bla = System.IO.Directory.GetFiles("C:\\", "*", System.IO.SearchOption.AllDirectories);

This line will most likely always fail,
since the HDD contains a folder which you can't access under normal conditions.
Ah and my bad it doesn't return an empty array it just throws an exception.
 
I suppose the easiest way would be to recurse the sub folders yourself and either check permissions first or simply catch and ignore any permission related errors.
 
I had a similar issue; I thought I could save myself alot of time using this:

Code:
String[] files = System.IO.Directory.GetFiles("C:\\", "*.*" SearchAll);

This will throw a UnauthorizedAccessException() on operating system hidden folders (even if you have them turned off)

The only solution I found was this:

Code:
List<String> files = new List<String>();
void DirSearch(string sDir)
        {
            try
            {
                String[] dirs = System.IO.Directory.GetDirectories(sDir);
                foreach (String d in dirs)
                {
                    String[] files = System.IO.Directory.GetFiles(d);
                    foreach (String file in files)
                    {
                        try
                        {
                            Files.Add(fd);
                        }
                        catch (System.IO.IOException ioex)
                        {
                            //
                        }
                        catch (System.UnauthorizedAccessException uaex)
                        {
                            //
                        }
                    }
                    DirSearch(d);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
        }
 
Back
Top