c#没有复制目录的代码,需要通过递归实现复制目录:

使用方法:

1、把c:\temp\index目录下的所有子目录和文件复制到 c:\temp\newindex目录下。

bool copy = copydirectory(“c:\\temp\\index\\”, “c:\\temp\\newindex\\”, true);

2、具体代码实现

需要引用system.io命名空间,实现代码如下:

private static bool copydirectory(string sourcepath, string destinationpath, bool overwriteexisting)  
{  
   bool ret = false;  
   try  
   {  
       sourcepath = sourcepath.endswith(@"\") ? sourcepath : sourcepath + @"\";  
       destinationpath = destinationpath.endswith(@"\") ? destinationpath : destinationpath + @"\";  

       if (directory.exists(sourcepath))  
       {  
           if (directory.exists(destinationpath) == false)  
               directory.createdirectory(destinationpath);  

           foreach (string fls in directory.getfiles(sourcepath))  
           {  
               fileinfo flinfo = new fileinfo(fls);  
               flinfo.copyto(destinationpath + flinfo.name, overwriteexisting);  
           }  
           foreach (string drs in directory.getdirectories(sourcepath))  
           {  
               directoryinfo drinfo = new directoryinfo(drs);  
               if (copydirectory(drs, destinationpath + drinfo.name, overwriteexisting) == false)  
                   ret = false;  
           }  
       }  
       ret = true;  
   }  
   catch (exception ex)  
   {  
       ret = false;  
   }  
   return ret;  
}
3、直接把代码拷贝到你的程序就大功告成

以上仅为记录,转载来源为:https://www.cnblogs.com/meetrice/p/5397289.html