原文地址:http://blog.csdn.net/hk_5788/article/details/52313100
1、这次修改了部分 交互 功能。新增清除替换源文件 、 去除程序启动提示、使用线程完成替换文件
效果图:
2、线程源码
#region 线程执行替换文件
/// <summary>
/// 替换文件
/// </summary>
private void ThreadReplaceFile()
{
替换文件
string tempPath = tbAimPath.Text;
遍历替换源文件
getAllFiles(tempPath + "\\");
---提示
MessageBox.Show("本次替换文件个数: " + iReplaceCount.ToString());
}
#endregion
3、详细源码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace replaceFile
{
public partial class Form1 : Form
{
#region 属性
/// <summary>
/// 源文件
/// </summary>
private string strAimReplaceName = string.Empty;
/// <summary>
/// 源文件路径
/// </summary>
private string strAimReplaceFilePath = string.Empty;
/// <summary>
/// 替换 文件个数
/// </summary>
private int iReplaceCount = 0;
#endregion
#region 构造函数
public Form1()
{
InitializeComponent();
}
#endregion
#region 控件函数
/// <summary>
/// 窗口加载函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 浏览按钮被单击,添加 替换目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnScan_Click(object sender, EventArgs e)
{
folderBrowserDialog1.Description = "请选择目录";
获取选择的路径
if (DialogResult.OK == folderBrowserDialog1.ShowDialog())
{
显示添加的目录
tbAimPath.Text = folderBrowserDialog1.SelectedPath;
}
}
/// <summary>
/// 窗口dragDrop事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_DragDrop(object sender, DragEventArgs e)
{
固定写法,不用改
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
}
else
{
e.Effect = DragDropEffects.None;
}
}
/// <summary>
/// 获取万文件名和文件路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_DragEnter(object sender, DragEventArgs e)
{
获取文件路径(含有文件名及文件格式)
string filePath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
//全路径赋值
this.strAimReplaceFilePath = filePath;
// 获取文件名
this.strAimReplaceName = this.strAimReplaceFilePath.Remove(0, this.strAimReplaceFilePath.LastIndexOf("\\") + 1);
显示获取的文件名
tbOrignFileName.Text = filePath;
}
--------------------------------------------------------------------
获取指定的目录中的所有文件(包括文件夹)
public void getAllFiles(string directory)
{
获取指定的目录中的所有文件(不包括文件夹)
this.getFiles(directory);
获取指定的目录中的所有目录(文件夹)
this.getDirectory(directory);
}
获取指定的目录中的所有文件(不包括文件夹)
public void getFiles(string directory)
{
string[] path = System.IO.Directory.GetFiles(directory);
for (int i = 0; i < path.Length; i++)
{
判断是否含有指定的文件名
if (path[i].Contains(this.strAimReplaceName))
{
iReplaceCount += 1;
替换文件
File.Copy(this.strAimReplaceFilePath, path[i], true);
显示文件名
lbMessage.Items.Add("替换文件:> " + path[i]);
}
}
}
获取指定的目录中的所有目录(文件夹)
public void getDirectory(string directory)
{
string[] directorys = System.IO.Directory.GetDirectories(directory);
如果该目录总没有其他文件夹
if (directorys.Length <= 0)
{
return;
}
else
{
for (int i = 0; i < directorys.Length; i++)
{
getAllFiles(directorys[i]);
}
}
}
=====================================================
/// <summary>
/// 替换按钮执行事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReplace_Click(object sender, EventArgs e)
{
try
{
检查路径是否合理
if (tbAimPath.Text == string.Empty)
{
MessageBox.Show("替换路径不能为空");
return;
}
检查文件是被否添加了
if (tbOrignFileName.Text == string.Empty)
{
MessageBox.Show("替换源文件没有选择");
return;
}
string tempStr = this.strAimReplaceFilePath;
比较助手的路径 与 替换源文件是否在同一个目录。原则:不能在同一个目录下
if (Environment.CurrentDirectory == tempStr.Remove(tempStr.LastIndexOf("\\") + 1, this.strAimReplaceName.Length))
{
MessageBox.Show("助手不能与替换源文件在同一个目录下");
return;
}
清屏
this.ClearLixtBox();
替换文件
string tempPath = tbAimPath.Text;
遍历替换源文件
getAllFiles(tempPath + "\\");
MessageBox.Show("本次替换文件个数: " + this.iReplaceCount.ToString());
计数器清零
this.iReplaceCount = 0;
}
catch (Exception ex)
{
MessageBox.Show("其他异常: " + ex.Message);
this.Close();
//throw;
}
}
/// <summary>
/// 清除listbox中的数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClearListBox_Click(object sender, EventArgs e)
{
清除数据
ClearLixtBox();
}
/// <summary>
/// 清除替换源文件输入框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClearOrignFile_Click(object sender, EventArgs e)
{
if (tbOrignFileName.Text != string.Empty)
{
tbOrignFileName.Text = string.Empty;
}
}
#endregion
#region 清除listbox
/// <summary>
/// 清除listbox
/// </summary>
private void ClearLixtBox()
{
有内容了,清屏
if (lbMessage.Items.Count > 0)
{
lbMessage.Items.Clear();
}
//else
//{
// MessageBox.Show("没有内容可以被清除");
//}
}
#endregion
}
}