result - How to copy all files from one folder to another folder using C#? -


using system.io;  namespace consoleapplication3 {     public class simplefilecopy     {         static void main()         {             string sourcepath = @"c:\source\";             string targetpath = @"c:\files\";              if (directory.exists(sourcepath))             {                 directoryinfo dirinfo = new directoryinfo(sourcepath);                 directoryinfo[] listofsubdir=dirinfo.getdirectories();                 foreach (var item in listofsubdir)                 {                     var childitem = item.fullname + "\\" + "c1";                     string[] files = system.io.directory.getfiles(childitem);                     foreach (var file in files)                     {                         console.writeline(file);                      }                   }                  console.read();              }         }     } } 

copying msdn, there following solution. recursively copies directories on source destination.

using system; using system.io;  class directorycopyexample {     static void main()     {         // copy current directory, include subdirectories.         directorycopy(".", @".\temp", true);     }      private static void directorycopy(string sourcedirname, string destdirname, bool copysubdirs)     {         // subdirectories specified directory.         directoryinfo dir = new directoryinfo(sourcedirname);         directoryinfo[] dirs = dir.getdirectories();          if (!dir.exists)         {             throw new directorynotfoundexception(                 "source directory not exist or not found: "                 + sourcedirname);         }          // if destination directory doesn't exist, create it.          if (!directory.exists(destdirname))         {             directory.createdirectory(destdirname);         }          // files in directory , copy them new location.         fileinfo[] files = dir.getfiles();         foreach (fileinfo file in files)         {             string temppath = path.combine(destdirname, file.name);             file.copyto(temppath, false);         }          // if copying subdirectories, copy them , contents new location.          if (copysubdirs)         {             foreach (directoryinfo subdir in dirs)             {                 string temppath = path.combine(destdirname, subdir.name);                 directorycopy(subdir.fullname, temppath, copysubdirs);             }         }     } } 

Comments