关于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);
}
}
/////////////////////////////////////////////
}
}
关于C#文件复制(递归)的更多相关文章
- java实现文件夹(包括其中的子文件夹、子文件)的复制——递归
这是学校java课的一道实验题,题目如下:编程,根据指定的源和目标位置,完成指定文件或文件夹(包括其中的子文件夹.子文件)的复制. 以下是我的实现,使用了递归: package com.simon.m ...
- 递归、字节流、文件复制_DAY20
1:递归(理解) (1)方法定义中调用方法本身的现象. (2)递归注意事项: A:要有出口,否则就是死递归. B:次数不能太多,否则内存溢出. 特殊事项:构造方法不能递归定义. 例子:cn.itcas ...
- java基础 File 递归删除文件夹中所有文件文件夹 目录(包含子目录)下的.java文件复制到e:/abc文件夹中, 并统计java文件的个数
File 递归删除文件夹中所有文件文件夹 package com.swift.kuozhan; import java.io.File; import java.util.Scanner; /*键盘录 ...
- java 打印流 递归复制子文件子文件夹 不同编码文件复制到同一文件中 序列化流反序列化流
package com.swift.jinjie; import java.io.BufferedInputStream; import java.io.File; import java.io.Fi ...
- IO复制多级目录 控制台输入文件目录然后把目录下java文件复制到 D: 并统计java个数
package cn.itcast_05; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...
- python实现某目录下将多个文件夹内的文件复制到一个文件夹中
现实生活中,我们经常有这样的需求,如下图,有三个文件夹,文件夹1内含有1.txt文件 文件夹2中内含有2.txt文件,文件夹3中含有3.txt文件.我们有时候需要把1.txt, 2.txt, 3.tx ...
- Linux学习总结(十)-文件复制及查看, 环境变量
一 文件复制及移动 1.命令 cp --------copy 的意思格式 cp 选项 源文件 目标文件a: 对于文件我们直接cp 文件 目标文件假定我们在普通用户家目录下/home/lv新建两个普通文 ...
- C# 把一个文件夹下所有文件复制到另一个文件夹下 把一个文件夹下所有文件删除(转)
C# 把一个文件夹下所有文件复制到另一个文件夹下 public static void CopyDirectory(string srcPath, string destPath) { try { ...
- Linux 文件复制命令cp
文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [option] source1 source2 source3 ... ...
- Linux 中 cp 命令(文件复制)
cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录.它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下.cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文 ...
随机推荐
- Specified key was too long; max key length is 767 b
alter table - engine=innodb,row_format=dynamic; Specified key was too long; max key length is 767 b
- mac下配置gdb调试golang
mac下配置gdb调试golang 原文链接 https://sourceware.org/gdb/wiki/BuildingOnDarwin Building GDB for Darwin Crea ...
- Devstack single node Installation on VM
Last three days, I want to install devstack on my virtual machine on Vmware Workstation.The VM'syste ...
- c++ 定义宏常量
#ifndef __GAME_DATA_H__ #define __GAME_DATA_H__ #ifndef GAME_IS_CREATE_SHOP #define GAME_IS_CREATE_S ...
- android 5.0 -- Ripple 效果
Ripple 水波纹效果,也就是涟漪效果. 波纹效果有两种: 1,波纹有边界:波纹涟漪效果只是显示在控件内部 android:background="?android:attr/select ...
- 常用JavaScript字符串方法简述
网址来源:http://www.html-js.com/article/JS-rookie-in-the-rookie-to-start-learning-to-fly-the-commonly-us ...
- 弹性布局EM的计算方法
文章来源: http://www.w3cplus.com/css/px-to-em 总结: 1.浏览器默认的字体大小为16PX,即1em 2.EM可以指定小数点的后三位 3.元素自身没有设置字体大小, ...
- 整体认识flume:Flume介绍、分布式安装、常见问题及解决方案
问题导读 1.什么是flume? 2.flume包含哪些组件? 3.Flume在读取utf-8格式的文件时会出现解析不了时间戳,该如何解决? Flume是一个分布式.可靠.和高可用的海量日志采集.聚合 ...
- php 导出 Excel 报错 exception 'PHPExcel_Calculation_Exception' with message
exception 'PHPExcel_Calculation_Exception' with message '粉丝数据!C2679 -> Formula Error: Operator '= ...
- input美化上传按钮美化
今天工作需求碰到 样式改变上传按钮 效果: <a href="javascript:;" class="a-upload"> <input t ...