zoukankan      html  css  js  c++  java
  • 复制文件夹下所有文件

    复制文件夹下所有文件,以及目录结构;一个递归,其他没什么了,直接上代码吧

     1 private static bool CopyFile(string path,string newPath) {
     2             string[] di = Directory.GetDirectories(path);
     3             string[] fi = Directory.GetFiles(path);
     4 
     5             if (!Directory.Exists(newPath)) {
     6                 Directory.CreateDirectory(newPath);
     7             }
     8 
     9             for (int i = 0; i < fi.Length; i++)
    10             {
    11                 string name = fi[i].Substring(fi[i].LastIndexOf("\") + 1);
    12                 
    13                 File.Copy(fi[i], newPath + "\" + name,true);
    14             }
    15             foreach (string p in di)
    16             {
    17                 string ps = p.Substring(p.LastIndexOf("\"));
    18                 CopyFile(p, newPath+ps+"");
    19             }
    20             return true;
    21         }

     我在这里针对上面的代码做一些修改,这要感谢我的一位开源中国的网友,是他提醒了我,这里我就直接引用他的话了

    话说这行
    string name = fi[i].Substring(fi[i].LastIndexOf("\") + 1);
    改为:string name = Path.GetFileName(fi[i]);
    更好吧

    stringps = p.Substring(p.LastIndexOf("\"));
    这个也类似

    newPath + "\" + name
    这个也应改为System.IO.Path.Combine(newPath, name)
    以支持跨平台

    针对上面我写的代码,我表示很遗憾,这确实是一大失误,还需要好好学习

  • 相关阅读:
    java实现算年龄
    java实现手机尾号评分
    java实现手机尾号评分
    java实现手机尾号评分
    java实现手机尾号评分
    java实现手机尾号评分
    java实现三角螺旋阵
    Delphi ActiveForm发布全攻略
    序列化FastReport,重要提示少走弯路 good
    深入探索ScrollWindow
  • 原文地址:https://www.cnblogs.com/xufei/p/filecopy.html
Copyright © 2011-2022 走看看