文件Copy和文件夹Copy

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//源目录
string sourceDirectory = @"E:\\Program"; //目标目录
string destDirectory = @"E:\\Test";
//拷贝目录
copyDirectory(sourceDirectory, destDirectory);
Console.WriteLine("拷贝完毕");
Console.ReadLine();
}
public static void copyDirectory(string sourceDirectory, string destDirectory)
{
//判断源目录和目标目录是否存在,如果不存在,则创建一个目录
if (!Directory.Exists(sourceDirectory))
{
Directory.CreateDirectory(sourceDirectory);
}
if (!Directory.Exists(destDirectory))
{
Directory.CreateDirectory(destDirectory);
}
//拷贝文件
copyFile(sourceDirectory, destDirectory); //拷贝子目录
//获取所有子目录名称
string[] directionName = Directory.GetDirectories(sourceDirectory); foreach (string directionPath in directionName)
{
//根据每个子目录名称生成对应的目标子目录名称
string directionPathTemp = destDirectory + "\\" + directionPath.Substring(sourceDirectory.Length + ); //递归下去
copyDirectory(directionPath, directionPathTemp);
}
}
public static void copyFile(string sourceDirectory, string destDirectory)
{
//获取所有文件名称
string[] fileName = Directory.GetFiles(sourceDirectory); foreach (string filePath in fileName)
{
//根据每个文件名称生成对应的目标文件名称
string filePathTemp = destDirectory + "\\" + filePath.Substring(sourceDirectory.Length + ); //若不存在,直接复制文件;若存在,覆盖复制
if (File.Exists(filePathTemp))
{
File.Copy(filePath, filePathTemp, true);
}
else
{
File.Copy(filePath, filePathTemp);
}
}
}
}
}

文件Copy和文件夹Copy的更多相关文章

  1. Java 实现文件上传、下载、打包、文件copy、文件夹copy。

    文件and文件夹copy package org.test; import java.io.*; public class FileCopy { /** * 复制单个文件 * * @param old ...

  2. c# 复制整个文件夹的内容,Copy所有文件

    /// <summary> /// 文件夹下所有内容copy /// </summary> /// <param name="SourcePath"& ...

  3. iOS开发文件夹--Copy items if needed

    蓝色文件夹 蓝色文件夹(folder)一般作为资源文件夹使用,与黄色文件夹的主要区别是不参与编译,所以说如果你在这些文件夹下编写的逻辑代码是不参与编译的,其他文件也不能直接引用它们,若引用其中文件需要 ...

  4. 多任务案例--文件夹copy.py

    import os import multiprocessing def copy_file(q,file_name,new_folder_name,old_folder_name): with op ...

  5. cmd copy命令 文件复制【转】

    本文转载自:https://www.jb51.net/article/18981.htm copy,中文含义为“复制”,一个很容易见名知意的命令,它的作用是复制文件,用法十分简单:copy 源文件 目 ...

  6. 工作总结 1 sql写法 insert into select from 2 vs中 obj文件和bin文件 3 npoi 模板copy CopySheet 最好先全部Copy完后 再根据生成sheet写数据 4 sheet.CopyRow(rowsindex, rowsindex + x); 5 npoi 复制模板如果出现单元格显示问题

    我们可以从一个表中复制所有的列插入到另一个已存在的表中: INSERT INTO table2SELECT * FROM table1; 或者我们可以只复制希望的列插入到另一个已存在的表中: INSE ...

  7. Roslyn 如何使用 MSBuild Copy 复制文件

    本文告诉大家如何在 MSBuild 里使用 Copy 复制文件 需要知道 Rosyln 是 MSBuild 的 dotnet core 版本. 在 MSBuild 里可以使用很多命令,本文告诉大家如何 ...

  8. Python [习题] 文件操作:目录间copy 文件

    [习题] 指定一个源文件,实现copy到目标目录.例如把/tmp/sample1.txt 拷贝到/tmp/sample2.txt原文件需要有读权限(默认rt权限),目标文件需要给写(w即可)权限. I ...

  9. C# 文件copy和文件删除

    C# 文件copy和文件删除 public bool CopyFile(string SourcePath, string CopyPathFoder) { bool bfg = false; if ...

随机推荐

  1. 最小二乘法 python实现

    #-*-coding:UTF-8-*- # Created on 2015年10月20日 # @author: hanahimi import numpy as np import random im ...

  2. html5的结构标记与内联元素

    <article>标签定义外部的内容(结构元素)<section>标签定义文档中的节(section,区段).比如章节,页眉,页脚或文档中的其他部分(结构元素)<nav& ...

  3. [USACO11JAN]利润Profits

    题目描述 The cows have opened a new business, and Farmer John wants to see how well they are doing. The ...

  4. vimium 使用心得

    首先,建议记住chrom自己的支持的快捷键 https://support.google.com/chrome/answer/157179?hl=zh-Hans&hlrm=en-GB& ...

  5. (转) ICML2016 TUTORIAL参会分享

    ICML2016 TUTORIAL参会分享 本文转自: https://mp.weixin.qq.com/s?__biz=MzI3MDE4NTk4MQ==&mid=2658399541& ...

  6. 新手教程之:循环网络和LSTM指南 (A Beginner’s Guide to Recurrent Networks and LSTMs)

    新手教程之:循环网络和LSTM指南 (A Beginner’s Guide to Recurrent Networks and LSTMs) 本文翻译自:http://deeplearning4j.o ...

  7. CentOS6.4安装Golang环境

    什么是Go? Go是一门并发支持.垃圾回收的编译型的系统编程语言,目的是为了创造一门具有在静态编译语言的高性能和动态语言的高效开发之间拥有良好平衡点的一门编程语言. Go的主要特点有哪些? 类型安全和 ...

  8. FreeSWITCH在呼叫失败的情况下播放语音提示

    看到好多网友问到这个问题.一般我们在打电话时会听到“您拨的电话正在通话中,请稍后再拨....”,或“电话无应答...”之类的提示,我们在 freeswitch 里也可以这样做. 其实很简单,默认的配置 ...

  9. Windows Kernel Security Training Courses

    http://www.codemachine.com/courses.html#kerdbg Windows Kernel Internals for Security Researchers Thi ...

  10. Unity脚本系统

    什么是脚本?脚本是一个 MonoBehavior, 继承关系是 MonoBehavior -> Behavior -> Component -> Object GameObject ...