编程语言
首页 > 编程语言> > C#拷贝整个文件夹以及子目录和其中文件

C#拷贝整个文件夹以及子目录和其中文件

作者:互联网

   private void CopyDirectory(string srcPath, string desPath)

        {             string folderName = srcdir.Substring(srcdir.LastIndexOf("\\")+1);               string desfolderdir = desPath +"\\"+ folderName;               if (desdir.LastIndexOf("\\") == (desPath.Length - 1))             {                 desfolderdir = desPath + folderName;             }             string[] filenames = Directory.GetFileSystemEntries(srcPath);               foreach (string file in filenames)
            {                 if (Directory.Exists(file))
                {                       string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);                     if (!Directory.Exists(currentdir))                     {                         Directory.CreateDirectory(currentdir);                     }                       CopyDirectory(file, desfolderdir);                 }                   else 
                {                     string srcfileName = file.Substring(file.LastIndexOf("\\")+1);                       srcfileName = desfolderdir + "\\" + srcfileName;                         if (!Directory.Exists(desfolderdir))                     {                         Directory.CreateDirectory(desfolderdir);                     }                                                                 File.Copy(file, srcfileName);                 }             }
       }

标签:LastIndexOf,string,C#,子目录,desfolderdir,desPath,文件夹,file,Directory
来源: https://www.cnblogs.com/LCLBook/p/10997206.html