About C # File replication (recursive)

Source: Internet
Author: 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 + "\ \ To create a directory to build. 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 () = = ")
{
Messag Ebox.show ("Please set" + Txtfilepath.text);
return;
}
Try
{
string[] filepathlist = File.ReadAllLines (txtfilepath.text);
for (int i = 0; i < FILEPATHL Ist. 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.A Pplication. StartupPath + "\ \ To create a directory that is built in. 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>
Move its contents from one directory to another
</summary>
<param name= "P" > Source directory </param>
<param name= "p_2" > Purpose directory </param>
private void Movefolderto (string p, string p_2)//file Level two replication
{
Try
{
Check if the destination directory exists
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 ();//Get destination first-level subdirectory-----------

Move the file first.
DirectoryInfo info = new DirectoryInfo (p);
fileinfo[] Files = info. GetFiles ();//Get source first-level sub-file---------
A layer file name under foreach (FileInfo file in files)//source folder
{

foreach (DirectoryInfo dirinfo in subdirectories)//Just destination sub-directory
{
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); Copy files to destination first-level subdirectory (true to overwrite files with the same name)
}

}
Label1. Text = "Copy Complete";
}
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 Openfiledialo g ();
File03. ShowDialog ();
This.txtSource.Text = file03. FileName;

}
private void Btncpfile_click (object sender, EventArgs e)
{
//file.copy (source file address, destination address, true);
Movefolderto (Txtsource.text, txtdestination.text);//file Level two replication
}

private void Btncpdirfile_click (object sender, EventArgs e)
{
CopyFolder (Txtsource.text, txtdestination.text);//Recursive replication
}
public string CopyFolder (string spath, String dpath)//recursive replication
{
String flag = "Success";
Try
{
Create destination Folder
if (! Directory.Exists (Dpath))
{
Directory.CreateDirectory
(Dpath);
}
Copy files
DirectoryInfo SDir = new DirectoryInfo (spath);
fileinfo[] Filearray = Sdir.getfiles ();
foreach (FileInfo file in Filearray) {
File. CopyTo (Dpath + "\ \" + file.) Name, True);
}
Looping sub-folders
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;
}
/////////////////////////////////////////////

}
}

About C # File replication (recursive)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.