zoukankan      html  css  js  c++  java
  • c#复制文件、文件夹代码

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

    使用方法:

    1、把c: empindex目录下的所有子目录和文件复制到 c: emp ewindex目录下。

    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
  • 相关阅读:
    Go 学习之旅
    IdentityServer4 3.1.x 迁移到 4.x
    Redash 二开
    frp 内网穿透远程桌面(Windows 10)配置
    Redash 二开
    Redash 二开
    Nginx 强制 HTTPS 配置
    ASP.NET Core 奇淫技巧之SPA部署
    .NET Core 对接微信小程序数据解密
    19c生产enq: FB
  • 原文地址:https://www.cnblogs.com/kangao/p/10935411.html
Copyright © 2011-2022 走看看