public static void CopyDirectory (String srcpath, String destpath) {try {
DirectoryInfo dir = new DirectoryInfo (Srcpath);
filesysteminfo[] FileInfo = dir. Getfilesysteminfos (); Get files and subdirectories under directory (without subdirectories) foreach (FileSystemInfo i in FileInfo) { if (I am DirectoryInfo) //Determine if folder {
if (! Directory.Exists (destpath+ "\ \" +i.name)) { directory.createdirectory (destpath + "\" + i.name); The folder that does not exist under the target directory is created as a subfolder} copydir (I.fullname, destpath + "\ \" + i.name); Recursive calls to copy subfolders } else { file.copy (i.fullname, DestPath + "\" + i.name,true); Copy File not folder, true means can overwrite file with same name }} } catch (Exception e) { throw; }
}
Before calling the CopyDirectory method, you can determine whether the original path and the target path exist.
if (directory.exists (Srcpath) &&directory.exists (DestPath)) { copydirectory (Srcpath,destpath);
}
Original address: http://www.cnblogs.com/iamlucky/p/5996222.html
C # Delete all files under a folder
public static void Delectdir (String srcpath)
{ try { DirectoryInfo dir = new DirectoryInfo (srcpath); filesysteminfo[] FileInfo = dir. Getfilesysteminfos (); Returns all files and subdirectories in the directory foreach (FileSystemInfo i in FileInfo) { if (I am DirectoryInfo) //Determine if the folder { DirectoryInfo subdir = new DirectoryInfo (i.fullname); SubDir. Delete (true); Delete subdirectories and files } else { file.delete (i.fullname); Delete specified file } } } catch (Exception e) { throw; }
}
You can determine whether a folder exists before calling the Delectdir method
if (directory.exists (Srcpath)) { delectdir (srcpath);}
Original address: http://www.cnblogs.com/iamlucky/p/5997865.html
C # Copy all files under one folder to another folder delete all files under a folder (GO)