当前位置: 移动技术网 > IT编程>开发语言>.net > c#复制文件、文件夹代码

c#复制文件、文件夹代码

2019年05月29日  | 移动技术网IT编程  | 我要评论

神印王座下载txt下载,爱情果冻演员表,彩客网推荐雷乐汇

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网