c# - How can i avoid or pass over a directory that is access denied? -


i have method:

private void searchfordoc()         {             try             {                 outputtext = @"c:\temp\outputtxt";                 outputphotos = @"c:\temp\outputphotos";                 temptxt = @"c:\temp\txtfiles";                 tempphotos = @"c:\temp\photosfiles";                 if (!directory.exists(temptxt))                 {                     directory.createdirectory(temptxt);                 }                 if (!directory.exists(tempphotos))                 {                     directory.createdirectory(tempphotos);                 }                 if (!directory.exists(outputtext))                 {                     directory.createdirectory(outputtext);                 }                 if (!directory.exists(outputphotos))                 {                     directory.createdirectory(outputphotos);                 }                 t = environment.getenvironmentvariable("userprofile") + "\\documents";                 //string[] extensionstxt = { "*.txt" };//directory.getfiles(t, "*.txt", searchoption.alldirectories);                 //var textfiles = extensionstxt.selectmany(x => directory.getfiles(t, x));                 string[] textfiles = directory.getfiles(t, "*.txt", searchoption.alldirectories);                 (int = 0; < textfiles.length; i++)                 {                      //file.copy(txtfiles[i], temptxt + "\\" + path.getfilename(txtfiles[i]),true);                     fileinfo fi = new fileinfo((textfiles[i]));                     directoryinfo d = new directoryinfo(temptxt);                     long dirsize = dirsize(d);                     //if copying file take directory on 50mb don't                     if ((dirsize + fi.length) <= 8388608)//7340032)//52428800)                         fi.copyto(temptxt + "\\" + fi.name, true);                     else                         break;                  }                 compressions("textfiles.zip", temptxt, outputtext);                   s = environment.getenvironmentvariable("userprofile") + "\\pictures";                 string[] extensions = { "*.bmp", "*.jpg", "*.png", "*.gif" };//add extensions want filter first                 var photosfiles = extensions.selectmany(x => directory.getfiles(s, x));                 //string[] photosfiles = directory.getfiles(s, "*.*", searchoption.alldirectories);                 (int = 0; < photosfiles.toarray().length; i++)                 {                     fileinfo fi = new fileinfo((photosfiles.toarray()[i]));                     directoryinfo d = new directoryinfo(tempphotos);                     long dirsize = dirsize(d);                     //if copying file take directory on 50mb don't                     if ((dirsize + fi.length) <= 8388608)//7340032)//52428800)                         fi.copyto(tempphotos + "\\" + fi.name, true);                     else                         break;                 }                 compressions("photofiles.zip", tempphotos, outputphotos);              }             catch (exception err)             {                 logger.write("there exception" + environment.newline + err);                 sendemail.begininvoke(new action(() => { sendemail.enabled = true; }));             }         } 

the method first gets text files mydocuments directory , subdirectories:

t = environment.getenvironmentvariable("userprofile") + "\\documents"; 

the problem example i'm getting exception:

10/08/2013--18:54 ==> first time log file created  10/08/2013--18:54 ==> ***** operation started ***** 10/08/2013--18:54 ==> ***** copied windowsupdate log file ***** 10/08/2013--18:54 ==> ***** drivers text file have been created ***** 10/08/2013--18:54 ==> ***** hosts file have been created ***** 10/08/2013--18:54 ==> there exception system.unauthorizedaccessexception: access path 'c:\users\simbalip\documents\my music\' denied.    @ system.io.__error.winioerror(int32 errorcode, string maybefullpath)    @ system.io.filesystemenumerableiterator`1.addsearchabledirstostack(searchdata localsearchdata)    @ system.io.filesystemenumerableiterator`1.movenext()    @ system.collections.generic.list`1..ctor(ienumerable`1 collection)    @ system.io.directory.internalgetfiledirectorynames(string path, string userpathoriginal, string searchpattern, boolean includefiles, boolean includedirs, searchoption searchoption)    @ system.io.directory.internalgetfiles(string path, string searchpattern, searchoption searchoption)    @ diagnostic_tool_blue_screen.form1.searchfordoc() 

what should in case ? prefer pass on directory , keep moving on next one.

you should catch unauthorizedaccessexception , continue loop otherwise:

for (int = 0; < textfiles.length; i++) {     try     {        // code here     }     catch(unauthorizedaccessexception)     {        // nothing here     } } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -