.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
Post a Comment