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. nginx 403

    location / { autoindex on; } chown -R www-data:www-data /var/www usermod -a -G www-data rootnano /et ...

  2. ogg 初始化

    192.168.27.33test11ghdb11gtrandata: 同步delete,update 使用config 文件:同步表使用进程根据SCN号和RBA和主键同步##目的:数据定时同步,从源 ...

  3. opencv3.1自带demo的介绍和运行操作。转载

    opencv3.1自带demo的介绍和运行操作. 下列实验基本都试过,有些需要根据自己的电脑修改一些路径或者调试参数. 值得注意的是,控制台程序输入有时候要在图像所在的窗口输入相应的指令.我的电脑上安 ...

  4. HTML link标签media参数

    写html这么久了,今天才发现link标签还有个media参数,赶紧把它补回来,虽然现在没有用到,但是不能不知道它 定义和用法 media 属性规定被链接文档将显示在什么设备上. media 属性用于 ...

  5. 语言总结—C/C++

    参考<程序员面试宝典> 1. 基本概念 1.1 赋值语句 例1. 按位与操作,例如:a=3,b=3,a&b值等于 0011 & 0011 结果还是0011,那么值还是3: ...

  6. ARM驱动调试方法、思路总结、笔记

    驱动程序的调试一. 打印: prink, 自制proc文件UBOOT传入console=ttySAC0 console=tty11. 内核处理UBOOT传入的参数console_setup add_p ...

  7. Windows上安装MongoDB步骤

    事前准备: 1.在mongoDB官网下载.msi文件,我下的是社区版,下载地址:https://www.mongodb.com/download-center#community 2.点击msi文件安 ...

  8. js中checkbox反选

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  9. HTTP Basic Authentication认证的各种语言 后台用的

    访问需要HTTP Basic Authentication认证的资源的各种语言的实现 无聊想调用下嘀咕的api的时候,发现需要HTTP Basic Authentication,就看了下. 什么是HT ...

  10. 4、Math对象

    1.编辑html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...