zoukankan      html  css  js  c++  java
  • C# 拷贝指定文件夹下的所有文件及其文件夹到指定目录

    要拷贝的文件及其文件夹结构

    其中.lab文件不能覆盖

    /// <summary>
    /// 拷贝oldlab的文件到newlab下面
    /// </summary>
    /// <param name="sourcePath">lab文件所在目录(@"~labsoldlab")</param>
    /// <param name="savePath">保存的目标目录(@"~labs
    ewlab")</param>
    /// <returns>返回:true-拷贝成功;false:拷贝失败</returns>
    public bool CopyOldLabFilesToNewLab(string sourcePath, string savePath)
    {
        if (!Directory.Exists(savePath))
        {
            Directory.CreateDirectory(savePath);
        }
    
        #region //拷贝labs文件夹到savePath下
        try
        {
            string[] labDirs = Directory.GetDirectories(sourcePath);//目录
            string[] labFiles = Directory.GetFiles(sourcePath);//文件
            if (labFiles.Length > 0)
            {
                for (int i = 0; i < labFiles.Length; i++)
                {
                    if (Path.GetExtension(labFiles[i]) != ".lab")//排除.lab文件
                    {
                        File.Copy(sourcePath + "\" + Path.GetFileName(labFiles[i]), savePath + "\" + Path.GetFileName(labFiles[i]), true);
                    }
                }
            }
            if (labDirs.Length > 0)
            {
                for (int j = 0; j < labDirs.Length; j++)
                {
                    Directory.GetDirectories(sourcePath + "\" + Path.GetFileName(labDirs[j]));
    
                    //递归调用
                    CopyOldLabFilesToNewLab(sourcePath + "\" + Path.GetFileName(labDirs[j]), savePath + "\" + Path.GetFileName(labDirs[j]));
                }
            }
        }
        catch (Exception)
        {
            return false;
        }
        #endregion
        return true;
    }
  • 相关阅读:
    杨辉三角形II(Pascal's Triangle II)
    easyUI DataGrid 分页
    var, object, dynamic的区别以及dynamic的使用
    如何防止程序多次运行
    HTTP报文
    值类型,引用类型,栈,堆,ref,out
    Robotlegs框架1.5简介
    TOGAF架构内容框架之内容元模型(下)
    ZOJ 1204 一个集合能组成多少个等式
    画透明位图
  • 原文地址:https://www.cnblogs.com/zhyue93/p/FileCopy.html
Copyright © 2011-2022 走看看