C # Copy folder (copy home folder and do not copy home folder)

Source: Internet
Author: User

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)

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.