zoukankan      html  css  js  c++  java
  • c#复制包含子目录文件夹代码

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

    需要引用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;  
    }

    使用方法:

    bool copy = CopyDirectory("c:\temp\index\", "c:\temp\newindex\", true);

    上面的方法将把c: empindex目录下的所有子目录和文件复制到 c: emp ewindex目录下。

  • 相关阅读:
    python for selenium 数据驱动测试
    iframe 处理
    百度登录、退出示例
    js弹框处理
    window.alert弹出处理
    通过网页内容识别和处理弹出窗口
    通过识别标题处理弹出窗口
    通过名称识别和处理弹出窗口
    发现github官网进不去了
    开发环境的搭建-记录一下
  • 原文地址:https://www.cnblogs.com/meetrice/p/5397289.html
Copyright © 2011-2022 走看看