C# 文件批量重命名工具源码实例

出处:http://www.cnblogs.com/flydoos/archive/2011/10/29/2228300.html

 

C# 文件批量重命名工具源码实例

 

今天,群里有人问了一个关于批量重命名的问题,所以就写个Demo,需要的也来看看吧。没难度,直接贴源码,抛砖引玉,并且附上VS2010项目源码附件。写得不好的地方,希望大家指出来,以便日后改正。 (核心只是File.Move() ,呵呵)

 

本人正在学习WPF,谁有关于WPF制作电子阅读器、导出/导入 PDF、Excel、WORD 之类的资料,希望发给我一份,谢谢。邮箱:flydoos@vip.qq.com

C# 文件批量重命名工具源码实例

复制代码
//////////////////////////////////////////////////////////////////

//作者:牛A与牛C之间
//Q Q:1046559384   C#/Java技术交流群:96020642
//微博:http://weibo.com/flydoos

//博客:http://www.cnblogs.com/flydoos

//日期:2011-10-29

//////////////////////////////////////////////////////////////////
using System;
using System.Windows.Forms;
using System.IO;

namespace FileRename
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

private void btnOpenPath_Click(object sender, EventArgs e)
{
FolderBrowserDialog f1 = new FolderBrowserDialog();
if (f1.ShowDialog() == DialogResult.OK)
{
txtPath.Text = f1.SelectedPath;
}
}

private void btnRename_Click(object sender, EventArgs e)
{
if (txtPath.Text != "")
{
if (txtNew.Text != "")
{
string strOldPart = txtOld.Text.Trim();
string strNewPart = txtNew.Text.Trim();
DateTime StartTime = DateTime.Now;
try
{
DirectoryInfo di = new DirectoryInfo(txtPath.Text);
FileInfo[] filelist = di.GetFiles("*.*");
string strFileFolder = txtPath.Text;
int i = 0;
int TotalFiles = 0;
foreach (FileInfo fi in filelist)
{
string strOldFileName = fi.Name;
string strNewFileName = fi.Name.Replace(strOldPart, strNewPart);
string strNewFilePath = @strFileFolder + "\\" + strNewFileName;
filelist[i].MoveTo(@strNewFilePath);
TotalFiles += 1;
lstFiles.Items.Add("文件名:" + strOldFileName + "    已重命名为  " + strNewFileName + "");
i += 1;
}
DateTime EndTime = DateTime.Now;
TimeSpan ts = EndTime - StartTime;
Text = ("总耗时:" + ts.Hours + "时" + ts.Minutes + "分" + ts.Seconds + "秒" + ts.Milliseconds + "毫秒");
}
catch
{
MessageBox.Show("路径无效!");
}
}
else
{
MessageBox.Show("没有匹配字符");
}
}
else
{
MessageBox.Show("请先择择路径!");
}
}
}
}

复制代码

发表回复

登录... 后才能评论