码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-09-16 21:39:07      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

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;
}
/////////////////////////////////////////////

}
}

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

标签:

原文地址:http://www.cnblogs.com/alex-13/p/4814453.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!