关于C#文件复制(递归)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WF01_mkfile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
txtFilepath.Text = System.Windows.Forms.Application.StartupPath + "\\要创建建的目录.txt";
txtSource.Text = System.Windows.Forms.Application.StartupPath + "\\Source\\";
txtDestination.Text = System.Windows.Forms.Application.StartupPath + "\\Destination\\";

}

private void btnmkfile_Click(object sender, EventArgs e)
{
if (txtFilepath.Text.Trim() == "")
{
MessageBox.Show("请设置 " + txtFilepath.Text);
return;
}
try
{
string[] filepathList = File.ReadAllLines(txtFilepath.Text);
for (int i = 0; i < filepathList.Length; i++)
{
// Directory.CreateDirectory(string path);
DirectoryInfo dir = new DirectoryInfo(filepathList[i]);
dir.Create();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//txtFilepath.Text = System.Windows.Forms.Application.StartupPath + "\\要创建建的目录.txt";
//label1.Text = System.Windows.Forms.Application.StartupPath;
}

private void btnChoosefile_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.ShowDialog();
this.txtFilepath.Text = file.FileName;
}

/// <summary>
/// 从一个目录将其内容移动到另一目录
/// </summary>
/// <param name="p">源目录</param>
/// <param name="p_2">目的目录</param>
private void MoveFolderTo(string p, string p_2)//文件二级复制
{
try
{
//检查是否存在目的目录
if (!Directory.Exists(p_2))
Directory.CreateDirectory(p_2);
if (!Directory.Exists(p))
Directory.CreateDirectory(p);
DirectoryInfo thisOne = new DirectoryInfo(p_2);
DirectoryInfo[] subDirectories = thisOne.GetDirectories();//获得Destination一级子目录 -----------

//先来移动文件
DirectoryInfo info = new DirectoryInfo(p);
FileInfo[] files = info.GetFiles();//获得Source一级子文件---------
foreach (FileInfo file in files)//Source文件夹下的一层文件名
{

foreach (DirectoryInfo dirinfo in subDirectories)//只是Destination一级子目录
{
string p_2des = p_2 + dirinfo.Name.ToString();
//label1.Text = dirinfo.Name.ToString();
File.Copy(Path.Combine(p, file.Name), Path.Combine(p_2des, file.Name), true); //复制文件到Destination一级子目录(为true是覆盖同名文件)
}

}
label1.Text = "复制完成咯";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void btnChoosefile02_Click_1(object sender, EventArgs e)
{
OpenFileDialog file02 = new OpenFileDialog();
file02.ShowDialog();
this.txtSource.Text = file02.FileName;
}

private void btnChoosefile03_Click(object sender, EventArgs e)
{
OpenFileDialog file03 = new OpenFileDialog();
file03.ShowDialog();
this.txtSource.Text = file03.FileName;

}
private void btncpfile_Click(object sender, EventArgs e)
{
// File.Copy(源文件地址, 目标地址, true);
MoveFolderTo(txtSource.Text, txtDestination.Text);//文件二级复制
}

private void btncpdirfile_Click(object sender, EventArgs e)
{
CopyFolder(txtSource.Text, txtDestination.Text);//递归复制
}
public string CopyFolder(string sPath, string dPath)//递归复制
{
string flag = "success";
try
{
// 创建目的文件夹
if (!Directory.Exists(dPath))
{
Directory.CreateDirectory
(dPath);
}
// 拷贝文件
DirectoryInfo sDir = new DirectoryInfo(sPath);
FileInfo[] fileArray = sDir.GetFiles();
foreach (FileInfo file in fileArray) {
file.CopyTo(dPath + "\\" + file.Name, true);
}
// 循环子文件夹
DirectoryInfo dDir = new DirectoryInfo(dPath);
DirectoryInfo[] subDirArray = sDir.GetDirectories();
foreach (DirectoryInfo subDir in subDirArray)
{
CopyFolder(subDir.FullName, dPath + "//" + subDir.Name);
}
}
catch (Exception ex)
{
flag = ex.ToString();
}
return flag;
}
/////////////////////////////////////////////

}
}

时间: 2024-08-09 19:47:34

关于C#文件复制(递归)的相关文章

递归、字节流、文件复制_DAY20

1:递归(理解) (1)方法定义中调用方法本身的现象. (2)递归注意事项: A:要有出口,否则就是死递归. B:次数不能太多,否则内存溢出. 特殊事项:构造方法不能递归定义. 例子:cn.itcast.demo package cn.itcast; /* * 递归算法: * 自己调用自己. * 方法内定义:调用到什么程度,就不调用自己了.即递归出口. */ public class Demo { public static void main(String[] args) { method(5

java 打印流 递归复制子文件子文件夹 不同编码文件复制到同一文件中 序列化流反序列化流

package com.swift.jinjie; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintStream; /*从键盘输入一个文件夹路径,利用打印流将该文件夹下的所有文件(包括子文件夹)复制到D盘下temp文件夹下.*/ public class PrintAllT

文件复制二进制版

两种复制方式: FILE *src = fopen("src.txt","r"); FILE *dest = fopen("dest.txt","w"); Char buf[1024]={0}; While(!feof(src)){ Size_t size = fread(buf,1,sizeof(buf),src); Fwrite(buf,1,size,dest); } 采用 stat函数 #include <sys/

windows 与linux 下用C++读取sqlite实现文件复制(二)

2.分别查询读取sqlite表中的字段. 1 //在数据库中查询表: 2 // 执行SQL 3 char **dbResult; //是 char ** 类型,两个*号 4 int nRow, nColumn; 5 sprintf(sql, "select * from test1"); 6 int result = sqlite3_get_table( conndb, sql, &dbResult, &nRow, &nColumn, &err_msg

windows 与linux 下用C++读取sqlite实现文件复制(三)

5.实现文件复制 1 int CopyFile(char *SourceFile,char *NewFile) 2 { 3 ifstream in; 4 ofstream out; 5 in.open(SourceFile,ios::binary);//打开源文件 6 if(in.fail())//打开源文件失败 7 { 8 cout<<"Error 1: Fail to open the source file."<<endl; 9 in.close(); 1

文件复制工具类

package com.renmai.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputS

Linux scp文件复制

scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. 1.scp命令的用处: scp在网络上不同的主机之间复制文件,它使用ssh安全协议传输数据,具有和ssh一样的验证机制,从而安全的远程拷贝文件. 2.scp命令基本格式: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S pro

Linux学习总结(十)-文件复制及查看, 环境变量

一 文件复制及移动 1.命令 cp --------copy 的意思格式 cp 选项 源文件 目标文件a: 对于文件我们直接cp 文件 目标文件假定我们在普通用户家目录下/home/lv新建两个普通文件 touch 1.txt 2.txtecho "sfsfsfsdf" >> 1.txt 随便写点数据进去cp 1.txt 2.txt 此时会提示你是否覆盖,意思是,是否用1.txt里面的内容替换2.txt里面的内容.这里启动了了一个 -i 别名选项,用于同用户交互的安全选项.

Linux 中 cp 命令(文件复制)

cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录.它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下.cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文件参数必须是一个已经存在的目录,否则将出现错误. 语法 cp(选项)(参数) 选项 -a:此参数的效果和同时指定"-dpR"参数相同: -d:当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录: -f:强行复制文件或目录,不论目标文件或目录是否已存

Python常用模块——文件复制模块shutil

Python常用模块--文件复制模块shutil shutil模块 高级的文件.文件夹.压缩包处理模块 shutil.copyfileobj(fsrc, fdst) 将文件内容拷贝到另一个文件中 import shutil shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w')) shutil.copyfile(src, dst) 拷贝文件 shutil.copyfile('f1.log', 'f2.log') #目标文件无需存在