exception - How to skip to copy Read only Directories in C# -


i trying write code copy files give directory , sub directories. (only copy files not directories).

this far have

public static void copy(string sourcedir, string targetdir) {     directory.createdirectory(targetdir);             foreach(var file in directory.getfiles(sourcedir))         file.copy(file, path.combine(targetdir, path.getfilename(file)),true);      foreach(var directory in directory.getdirectories(sourcedir))         copy(directory, path.combine(targetdir, path.getfilename(directory))); } 

i facing 2 problems here.

  1. i unauthorized access exception. there folder in source directory creating problem.

    • enter image description here

2nd problem is. if copies copy files , folder in source directory. need files.

any solution both problems ?

ok copy sub directories, , on, omitting wont let have access:

before call copy, need check targetdir exists.

if (!directory.exists(targetdir)) directory.createdirectory(targetdir); 

you should check can etc, recursively copy folders same one:

public static void copy(string sourcedir, string targetdir) {   try   {     foreach(var file in directory.getfiles(sourcedir))     try        {  file.copy(file, path.combine(targetdir, path.getfilename(file)),true);}         catch {} // other things handle different issues - lack of space, etc      foreach(var directory in directory.getdirectories(sourcedir))         copy(directory, targetdir)); // go each subdirectory, , same.    }    catch {} // catches unable access directory you've gone  } 

}

you keep final destination same if wanted flat dump folder, trying append path etc copy recreating whole directory structure.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -