Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • Administrators
Posted

Would something like

       private List GetFiles(string path, params string[] patterns)
       {
           List files = new List();
           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?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Posted

I had a similar issue; I thought I could save myself alot of time using this:

 

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:

 

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);
           }
       }

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...