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
//////////////////////////////////////////////////////////////////
//作者:牛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("請先擇擇路徑!");
}
}
}
}
完整代碼下載:網盤下載1 網盤下載2 備用下載3
VS2010的項目,如果無法開啟,請到這裡下載 Visual Studio 版本互轉工具1.1 ,之後就能開啟了