C#實(shí)現(xiàn)的文件批量重命名功能示例
本文實(shí)例講述了C#實(shí)現(xiàn)的文件批量重命名功能。分享給大家供大家參考,具體如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//C#批量重命名文件代碼的實(shí)現(xiàn)
//添加文件操作空間引用
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog f1 = new FolderBrowserDialog();
if (f1.ShowDialog() == DialogResult.OK)
{
textBox3.Text = f1.SelectedPath;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text!=""){
if(textBox1.Text!="")
{
string strOldFileName;
string strNewFileName;
string strOldPart = this.textBox1.Text.Trim();
string strNewPart = this.textBox2.Text.Trim();
string strNewFilePath;
string strFileFolder;
int TotalFiles = 0;
DateTime StartTime = DateTime.Now;//獲取開(kāi)始時(shí)間
try{
DirectoryInfo di = new DirectoryInfo(textBox3.Text);
FileInfo[] filelist = di.GetFiles("*.*");
strFileFolder = textBox3.Text;
int i = 0;
foreach (FileInfo fi in filelist)
{
strOldFileName = fi.Name;
strNewFileName = fi.Name.Replace(strOldPart, strNewPart);
strNewFilePath = @strFileFolder + "\\" + strNewFileName;
filelist[i].MoveTo(@strNewFilePath);
TotalFiles += 1;
this.listBox1.Items.Add("文件名:" + strOldFileName + " 已重命名為 " + strNewFileName + "");
i += 1;
}
DateTime EndTime = DateTime.Now;//獲取結(jié)束時(shí)間
TimeSpan ts = EndTime - StartTime;
this.listBox1.Items.Add("總耗時(shí):" + ts.Hours.ToString() + "時(shí)" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒"+ ts.Milliseconds.ToString()+"毫秒");
}
catch
{
MessageBox.Show("路徑無(wú)效!");
}
}
else
{
MessageBox.Show("沒(méi)有匹配字符");
}
}
else
{
MessageBox.Show("請(qǐng)先擇擇路徑!");
}
}
}
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《C#文件操作常用技巧匯總》、《C#遍歷算法與技巧總結(jié)》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
Unity3D使用鼠標(biāo)旋轉(zhuǎn)縮放平移視角
這篇文章主要為大家詳細(xì)介紹了Unity3D使用鼠標(biāo)旋轉(zhuǎn)縮放平移視角,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
C#實(shí)現(xiàn)接口base調(diào)用示例詳解
這篇文章主要為大家介紹了C#實(shí)現(xiàn)接口base調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
unity實(shí)現(xiàn)動(dòng)態(tài)排行榜
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)動(dòng)態(tài)排行榜,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C#實(shí)現(xiàn)導(dǎo)入CSV文件到Excel工作簿的方法
這篇文章主要介紹了C#實(shí)現(xiàn)導(dǎo)入CSV文件到Excel工作簿的方法,涉及C#針對(duì)office組件的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06
WPF實(shí)現(xiàn)平面三角形3D運(yùn)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)平面三角形3D運(yùn)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09

