Copy home folder
private void CopyDirectory (string srcdir, String desdir) {try {if (F Ile. Exists (Srcdir)) {file.copy (Srcdir, Desdir); } else {string folderName = Srcdir. Substring (Srcdir. LastIndexOf ("\ \") + 1); String desfolderdir = Desdir + "\ \" + FolderName; if (Desdir. LastIndexOf ("\ \") = = (Desdir. Length-1)) {Desfolderdir = Desdir + folderName; } string[] filenames = directory.getfilesystementries (Srcdir); foreach (string file in filenames)//traverse all files and directories {if (directory.exists (file))// As directory processing, if the directory is present, recursively copy the file under the directory {string currentdir = Desfolderdir + "\ \" + file. Substring (file. LastIndexOf ("\ \") + 1); if (! Directory.Exists (Currentdir)) {Directory.CreateDirectory (Curren TDIR); } copydirectory (file, desfolderdir); } else//otherwise direct copy file {string srcfilename = file. Substring (file. LastIndexOf ("\ \") + 1); Srcfilename = desfolderdir + "\ \" + Srcfilename; if (! Directory.Exists (Desfolderdir)) {Directory.CreateDirectory (DESF OLDERDIR); } file.copy (File, srcfilename); }}}} catch (Exception ex) {Messagebo X.show (ex. ToString ()); } }
Do not copy home folder
private void CopyDirectory (string srcdir, String desdir) {try {if (F Ile. Exists (Srcdir)) {file.copy (Srcdir, Desdir); } else {string folderName = Srcdir. Substring (Srcdir. LastIndexOf ("\ \") + 1); string desfolderdir = Desdir; String desfolderdir = Desdir + "\ \" + FolderName; if (Desdir. LastIndexOf ("\ \") = = (Desdir. Length-1)) {Desfolderdir = Desdir + folderName; } string[] filenames = directory.getfilesystementries (Srcdir); foreach (string file in filenames)//traverse all files and directories {if (directory.exists (file))// As directory processing, if this directory is present, recursively copy the file under the directory {string currentdir = Desdir + "\ \" + fil E.substring (file.LastIndexOf ("\ \") + 1); if (! Directory.Exists (Currentdir)) {Directory.CreateDirectory (Curren TDIR); } Desfolderdir = Desdir + "\ \" + file. Substring (file. LastIndexOf ("\ \") + 1); CopyDirectory (file, desfolderdir); } else//otherwise direct copy file {string srcfilename = file. Substring (file. LastIndexOf ("\ \") + 1); Srcfilename = desfolderdir + "\ \" + Srcfilename; if (! Directory.Exists (Desfolderdir)) {Directory.CreateDirectory (DESF OLDERDIR); } file.copy (File, srcfilename); }}}} catch (ExceptIon ex) {MessageBox.Show (ex. ToString ()); } }
C # Copy folder (copy home folder and do not copy home folder)