c# 4.0 - Need a method for searching all of C drive for a directory in C# -


i need find folder called "gamedata", in filesystem, stored in program files, if not, able find it, wherever in c drive.

currently, i'm using:

ienumerable<string> list = directory.getdirectories(root, "gamedata", searchoption.alldirectories); 

but throws unauthorizedaccessexception expected, trying navigate through protected directories.

is there perhaps can add stop attempting access protected directories? try/catch block allow search continue after exception?

edit

note: i'm not looking specific file, folder called gamedata, can use location output .zip extraction, or read names of folders within.

you need use recursive method instead of alldirectories. skip directories cause exception.

msdn: iterate through directory tree (c# programming guide)

"the weakness in using searchoption.alldirectories if 1 of subdirectories under specified root causes directorynotfoundexception or unauthorizedaccessexception, whole method fails , returns no directories. same true when use getfiles method. if have handle these exceptions on specific subfolders, must manually walk directory tree"

static void walkdirectorytree(system.io.directoryinfo root) {     system.io.fileinfo[] files = null;     system.io.directoryinfo[] subdirs = null;      // first, process files directly under folder      try     {         files = root.getfiles("*.*");     }     // thrown if 1 of files requires permissions greater      // application provides.      catch (unauthorizedaccessexception e)     {         // code writes out message , continues recurse.          // may decide different here. example,          // can try elevate privileges , access file again.         log.add(e.message);     }      catch (system.io.directorynotfoundexception e)     {         console.writeline(e.message);     }      if (files != null)     {         foreach (system.io.fileinfo fi in files)         {             // in example, access existing fileinfo object. if              // want open, delete or modify file,              // try-catch block required here handle case              // file has been deleted since call traversetree().             console.writeline(fi.fullname);         }          // find subdirectories under directory.         subdirs = root.getdirectories();          foreach (system.io.directoryinfo dirinfo in subdirs)         {             // resursive call each subdirectory.             walkdirectorytree(dirinfo);         }     }             } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -