文件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. UI学习笔记---第五天

    target...action设计模式   代理设计模式   手势识别器 target...action设计模式 耦合是衡量一个程序写的好坏的标准之一,耦合是衡量模块与模块之间关联程度的指标 &quo ...

  2. 232. Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  3. C++ Primer : 第十二章 : 动态内存之unique_ptr和weak_ptr

    unique_ptr 一个unique_ptr拥有它所管理的对象,与shared_ptr不同,unique_ptr指向的对象只能有一个用户.当unique_ptr被销毁后,它所指向的对象也被销毁. 定 ...

  4. mybatis+spring的简单介绍学习

    参考下面链接 http://mybatis.github.io/spring/zh/index.html

  5. scala言语基础学习十二

  6. Codeforces Round #339 Div.2 C - Peter and Snow Blower

    Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. A ...

  7. 【转】IOS学习笔记29—提示框第三方库之MBProgressHUD

    原文网址:http://blog.csdn.net/ryantang03/article/details/7877120 MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单 ...

  8. I/O Completions port

    http://blogs.technet.com/b/winserverperformance/archive/2008/06/26/designing-applications-for-high-p ...

  9. apk反编译生成程序的源代码和图片、XML配置、语言资源等文件

    Android应用的UI越来越漂亮,遇到喜欢的我们可以通过反编译,得到应用的源代码借鉴下别人的思想. 具体步骤: 1.下载 apktool 下载地址:https://code.google.com/p ...

  10. Eclipse+Maven创建webapp项目<一><二><三>

    转-http://www.cnblogs.com/candle806/p/3439469.html Eclipse+Maven创建webapp项目<一> 1.开启eclipse,右键new ...