关于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;
using System.Net;
using System.Threading;

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\\";
txtURLFile.Text = System.Windows.Forms.Application.StartupPath + "\\CheckURL.txt";
txtIncludeChar.Text = "woclome to our admin cPanel";

}

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();
label1.Text = "文件夹创建OK了";
}
}
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一级子文件---------
string urllog = "";
foreach (FileInfo file in files)//Source文件夹下的一层文件名
{

foreach (DirectoryInfo dirinfo in subDirectories)//只是Destination一级子目录
{
Random rn = new Random();
string fileNamenew = "";
string p_2des = p_2 + dirinfo.Name.ToString();
if ((rn.Next(10) % 2) == 0)
{
fileNamenew = GenerateRandomChar(5) + Path.GetExtension(file.Name); ;//文件重命名为随机数.后缀
}
else
{
fileNamenew = GenerateRandomChar(8) + Path.GetExtension(file.Name); ;//文件重命名为随机数.后缀
}
urllog += "http://" + dirinfo.Name.ToString() + "/" + fileNamenew + System.Environment.NewLine;//记录换行
File.Copy(Path.Combine(p, file.Name), Path.Combine(p_2des, fileNamenew), true); //复制文件到Destination一级子目录(为true是覆盖同名文件)

}

}
StreamWriter SWriter = new StreamWriter(p_2+ "/logurl.txt");
SWriter.Write(urllog);
SWriter.Close();
label1.Text = "复制完成咯";
//lblNote.Text = GenerateRandomChar(6);
}
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;
}
/////////////////////////////////////////////
/// 生成随机字母字符串(数字字母混和)
///
/// 待生成的位数
/// 生成的字母字符串

private string GenerateRandomChar(int codeCount)
{
Random r = new Random();
string s = string.Empty;
string str = string.Empty;
for (int i = 0; i < codeCount; i++)
{
if ((r.Next(10) % 2) == 0)
{
s = ((char)r.Next(97, 123)).ToString();
}
else
{
s = ((char)r.Next(65, 90)).ToString();
}
str += s;
}
return str;
}
private Mutex mu = new Mutex(false, "wr");
public void Replacewrit(string writpath, string writbody)
{
mu.WaitOne();
System.IO.StreamWriter swlinks;
swlinks = new System.IO.StreamWriter(writpath, true, System.Text.Encoding.UTF8);
swlinks.Write(writbody);
swlinks.Close();
mu.ReleaseMutex();
}
private Mutex mut = new Mutex(false, "wr1");
public void Replacewrit(string writpath, bool append, string writbody)
{
mut.WaitOne();
System.IO.StreamWriter swlinks;
swlinks = new System.IO.StreamWriter(writpath, append, System.Text.Encoding.UTF8);
swlinks.Write(writbody);
swlinks.Close();
mut.ReleaseMutex();
}

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

private void btnCheckURL_Click(object sender, EventArgs e)
{
if (txtURLFile.Text.Trim() == "")
{
MessageBox.Show("请设置 " + txtURLFile.Text);
return;
}

try{
Thread t = new Thread(new ThreadStart(() =>
{
btnCheckURL.Invoke(new Action(() =>
{
btnCheckURL.Text = "检测中……";
btnCheckURL.Enabled = false;
}));
string[] fileURLFile = File.ReadAllLines(txtURLFile.Text);
string Url = "";
string URL_results = "";
for (int i = 0; i < fileURLFile.Length; i++)
{
try
{
WebClient myWebClient = new WebClient();
Url = fileURLFile[i];
lbl2.Invoke(new Action(() =>
{
lbl2.Text = "正检测 " + i + " -- " +Url + "\r\n";
//Replacewrit(System.Windows.Forms.Application.StartupPath + "\\seo\\日志.txt", state);
}));
//lbl2.Text = "正检测 " + i + Url + "\r\n";
Stream myStream = myWebClient.OpenRead(Url);
StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
string strHTML = sr.ReadToEnd();
myStream.Close();
if (strHTML.Contains(txtIncludeChar.Text))
{
Replacewrit(System.Windows.Forms.Application.StartupPath + "\\CheckURL_OK.txt", Url + "\r\n");
// URL_results += Url + "\r\n";
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);// lbl2.Text = "访问出错 " + Url + "\r\n";
lbl2.Invoke(new Action(() =>
{
lbl2.Text = "访问出错 " + i + " -- " + Url + "\r\n";
}));

}
//label1.Text = "文件夹创建OK了";
}
// StreamWriter SWriter = new StreamWriter(System.Windows.Forms.Application.StartupPath + "\\CheckURL_results.txt");
// SWriter.Write(URL_results);
// SWriter.Close();

txtURLFile.Invoke(new Action(() =>
{
btnCheckURL.Text = "开始检测";
btnCheckURL.Enabled = true;
}));
}));
t.IsBackground = true;
t.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

/////////////////////////////////////////////

}
}

posted @ 2015-09-16 20:47  也许明天  阅读(1281)  评论(0编辑  收藏  举报