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. VMMap(查看内存工具)

    来源: http://www.cnblogs.com/georgepei/archive/2012/03/07/2383445.html http://www.cnblogs.com/georgepe ...

  2. 浏览器exp使用经验

    0x00背景 windows平台下,浏览器安全是绕不过的话题,其涉及的安全问题涵盖二进制和web,攻击场景也非常多样化: 用户点击攻击者的恶意URL链接被感染恶意代码 访问恶意站点被绕过同源策略窃取c ...

  3. Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试【转】

    环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到下面的代码 LoadModule rewrite_module modules/mod_ ...

  4. @property (nonatomic, getter = isExpanded) BOOL expanded;

    如果这个property是 BOOL on, 那么Objc默认创建的 setter 为: - (void)on:(BOOL)setOn { } getter 为: - (BOOL)on { retur ...

  5. python多进程,以及进程池并发

    模拟多进程 #!/usr/bin/env python#-*- coding:utf-8 -*-import timefrom multiprocessing import Process def s ...

  6. zookeeper 安装 windows环境

    1.zookeeper下载地址: http://mirrors.cnnic.cn/apache/zookeeper/ 2.单点配置 把下载的zookeeper的文件解压到指定目录 修改conf下增加一 ...

  7. ZeroMQ中PUB-SUB模式测试

    因为公司有需求,对程序模块之间通信效率有较高的需求.之前公司用的通信组件是ActiveMQ,根据网上公布的测试结果显示其效率比较低, 后来考虑准备在新的项目中开始使用ZeroMQ.看了几天发现用起来比 ...

  8. 登录SQL注入

    在登录页面的账号密码的输入框中分别输入,这个值:1' or '1'='1 一,验证的数据库语句,讲传人的值组合成数据库语句: public DataTable CheckLogin(string na ...

  9. validform 怎么验证小数。

    <input type="text" class="input-text" value="{$info.score}" placeho ...

  10. hdu_5821_Ball(贪心)

    题目链接:hdu_5821_Ball 题意: 给你序列A和序列B,依次给你m个区间,每次你可以交换区间内的任意数,问最后能不能将A变成B 题解: 比赛当时没想到,智商被压制 1001: 假设有4个红球 ...