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

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

}
}

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

  1. java实现文件夹(包括其中的子文件夹、子文件)的复制——递归

    这是学校java课的一道实验题,题目如下:编程,根据指定的源和目标位置,完成指定文件或文件夹(包括其中的子文件夹.子文件)的复制. 以下是我的实现,使用了递归: package com.simon.m ...

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

    1:递归(理解) (1)方法定义中调用方法本身的现象. (2)递归注意事项: A:要有出口,否则就是死递归. B:次数不能太多,否则内存溢出. 特殊事项:构造方法不能递归定义. 例子:cn.itcas ...

  3. java基础 File 递归删除文件夹中所有文件文件夹 目录(包含子目录)下的.java文件复制到e:/abc文件夹中, 并统计java文件的个数

    File 递归删除文件夹中所有文件文件夹 package com.swift.kuozhan; import java.io.File; import java.util.Scanner; /*键盘录 ...

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

    package com.swift.jinjie; import java.io.BufferedInputStream; import java.io.File; import java.io.Fi ...

  5. IO复制多级目录 控制台输入文件目录然后把目录下java文件复制到 D: 并统计java个数

    package cn.itcast_05; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...

  6. python实现某目录下将多个文件夹内的文件复制到一个文件夹中

    现实生活中,我们经常有这样的需求,如下图,有三个文件夹,文件夹1内含有1.txt文件 文件夹2中内含有2.txt文件,文件夹3中含有3.txt文件.我们有时候需要把1.txt, 2.txt, 3.tx ...

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

    一 文件复制及移动 1.命令 cp --------copy 的意思格式 cp 选项 源文件 目标文件a: 对于文件我们直接cp 文件 目标文件假定我们在普通用户家目录下/home/lv新建两个普通文 ...

  8. C# 把一个文件夹下所有文件复制到另一个文件夹下 把一个文件夹下所有文件删除(转)

    C# 把一个文件夹下所有文件复制到另一个文件夹下   public static void CopyDirectory(string srcPath, string destPath) { try { ...

  9. Linux 文件复制命令cp

    文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [option] source1 source2 source3 ... ...

  10. Linux 中 cp 命令(文件复制)

    cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录.它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下.cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文 ...

随机推荐

  1. 常用mysql命令

    net start mysql命令,启动mysql数据库 1:查看服务器上存在哪些数据库:show databases;2:建立数据库mydb: create database mydb;3:使用你所 ...

  2. python 在mongo 中建立索引

    import pymongo mongo = pymongo.Connection('localhost') collection = mongo['database']['user'] collec ...

  3. Android KeyCode

    KEYCODE_UNKNOWN=0; KEYCODE_SOFT_LEFT=1; KEYCODE_SOFT_RIGHT=2; KEYCODE_HOME=3; KEYCODE_BACK=4; KEYCOD ...

  4. php 编程效率(3)

    提高php编程效率的53个小知识点:用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中 搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当 ...

  5. 二十六、oracle pl/sql 分页

    一.无返回值的存储过程 古人云:欲速则不达,为了让大家伙比较容易接受分页过程编写,我还是从简单到复杂,循序渐进的给大家讲解.首先是掌握最简单的存储过程,无返回值的存储过程. 案例:现有一张表book, ...

  6. jsvc 以daemon方式运行tomcat

    原理: 使用jsvc来运行服务,没有了默认8005的shutdown端口: 主进程pid为1,fork 2个进程 运行方式参考:http://commons.apache.org/proper/com ...

  7. this的相关知识

    一如既往,直接上代码: function fn(name,age){ var obj=new Object(); obj.name=name; obj.age=age; obj.talk=functi ...

  8. redis 5 种数据结构

    常用命令 就DB来说,Redis成绩已经很惊人了,且不说memcachedb和tokyocabinet之流,就说原版的memcached,速度似乎也只能达到这个级别.Redis根本是使用内存存储,持久 ...

  9. 第一百一十三节,JavaScript文档对象,DOM基础

    JavaScript文档对象,DOM基础 学习要点: 1.DOM介绍 2.查找元素 3.DOM节点 4.节点操作 DOM(Document Object Model)即文档对象模型,针对HTML和XM ...

  10. Java Object 对象拷贝

    Java Object 对象拷贝 @author ixenos JAVA 对象拷贝 Java里的clone分为:  1.浅拷贝:浅复制仅仅复制所考虑的对象,而不复制它所引用的对象,Object类里的c ...