關於C#檔案複製(遞迴)

來源:互聯網
上載者:User

標籤:

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#檔案複製(遞迴)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.