用C# 做的一个小而实用的 <文件重命名批处理> 工具

本文介绍了一款使用C#编写的简单实用的文件批量重命名工具,通过替换文件名中的一部分来实现快速重命名,适用于需要频繁进行文件名修改的工作场景。

当初因为要用到这种功能的工具,便从网上找了C# 重命名文件的方法,便自己做了一个。虽然简单,但挺实用的,我经常能用到

 

 

就一个文件代码:

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//C#批量重命名文件代码的实现
//添加文件操作空间引用
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;//获取开始时间
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;//获取结束时间
TimeSpan ts = EndTime - StartTime;
this.listBox1.Items.Add("总耗时:" + ts.Hours.ToString() + "" + ts.Minutes.ToString() + "" + ts.Seconds.ToString() + ""+ ts.Milliseconds.ToString()+"毫秒");
}
catch
{
MessageBox.Show(
"路径无效!");
}

}
else
{
MessageBox.Show(
"没有匹配字符");
}
}
else
{
MessageBox.Show(
"请先择择路径!");
}
}
}
}

 

转载于:https://www.cnblogs.com/Along729/archive/2010/12/21/rename.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值