.net - skip directories don't have required permission c# -


        private int getfilecount(directoryinfo directory)         {             int retval = directory.getfiles().length;              try             {                 array.foreach(directory.getdirectories(), dir =>                     {                         retval += getfilecount(dir);                     });             }             catch (unauthorizedaccessexception ex)             {                                 //here stops execution             }              return retval;         } 

i have above code written when scan through disk drive...giving me access denied exception.. handle exception written catch block stop further iteration..how should continue further iteration

inside catch block please check if retval null , if yes use continue keyword , increment counter value. here sample:

private int getfilecount( )     {              string[] subdirectories = directory.getdirectories(@"c:\");             string[] result = null;              if (subdirectories.length > 0)             {                 (int = 0; < subdirectories.length - 1; i++)                 {                     try                     {                         result = directory.getfiles(subdirectories[i]);                     }                     catch (unauthorizedaccessexception ex)                     {                         if (result == null)                         {                             += 1;                             continue;                         }                     }                 }             }                    return 1;     } 

Comments

Popular posts from this blog

c# - ItextSharp font color issue in ver 5.5.4+ -

jquery - Multiple issues with pushstate: history, loading, calling functions -

ios - retrievePeripherals deprecated in IOS7 how to substitude it with retrievePeripheralsWithIdentifiers -