C # Copy all files under one folder to another folder delete all files under a folder

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.