嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
批量修改文件名称
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace UpdateFileName
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
txtFolderPath.Text = folderBrowserDialog1.SelectedPath;
}
private void btnStart_Click(object sender, EventArgs e)
{
DoFileName(1);
}
private void DoFileName(int iDoType)
{
string str = txtFolderPath.Text.Trim();
if (string.IsNullOrEmpty(str))
{
MessageBox.Show("请选择文件夹!");
}
else if (!Directory.Exists(str))
{
MessageBox.Show("文件夹不存在,请重新选择!");
txtFolderPath.Text = string.Empty;
}
else
{
try
{
string str2 = txtReplaceZfc.Text.Trim();
string str3 = textBox1.Text.Trim();
string str5 = textBox2.Text.Trim();
string str6 = textBox3.Text.Trim();
string str7 = textBox4.Text.Trim();
string str8 = textBox5.Text.Trim();
string[] files = Directory.GetFiles(str);
if ((files == null) || (files.Length == 0))
{
MessageBox.Show("当前文件夹中不存在要修改的文件!");
}
else
{
string newValue = txtNewZfc.Text.Trim();
int i = 1;
foreach (string str4 in files)
{
var info = new FileInfo(str4);
switch (iDoType)
{
case 1:
if (info.Name.Contains(str2))
{
info.MoveTo(info.DirectoryName @"\" info.Name.Replace(str2, newValue));
i ;
}
break;
case 2:
if (str3 != "")
{
info.MoveTo(info.DirectoryName @"\" str3 info.Name);
i ;
}
break;
case 3:
if (str5 != "")
{
int ext = info.Name.LastIndexOf(".");
if (ext > -1)
{
info.MoveTo(info.DirectoryName @"\" info.Name.Substring(0, ext) str5 info.Name.Substring(ext, info.Name.Length - ext));
}
else
{
info.MoveTo(info.DirectoryName @"\" info.Name str5);
}
i ;
}
break;
case 4:
if (str6 != "")
{
int ext = info.Name.LastIndexOf(".");
if (ext > -1)
{
info.MoveTo(info.DirectoryName @"\" str6 str7 i str8 info.Name.Substring(ext, info.Name.Length - ext));
}
else
{
info.MoveTo(info.DirectoryName @"\" str6 str7 i str8);
}
i ;
}
else
{
int ext = info.Name.LastIndexOf(".");
if (ext > -1)
{
info.MoveTo(info.DirectoryName @"\" info.Name.Substring(0, ext) str7 i str8 info.Name.Substring(ext, info.Name.Length - ext));
}
else
{
info.MoveTo(info.DirectoryName @"\" info.Name str7 i str8);
}
i ;
}
break;
}
}
MessageBox.Show("共处理了 " (i - 1).ToString() " 个文件!");
}
}
catch (Exception exception)
{
MessageBox.Show("Error : " exception.Message);
throw exception;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
DoFileName(2);
}
private void button2_Click(object sender, EventArgs e)
{
DoFileName(3);
}
private void button3_Click(object sender, EventArgs e)
{
DoFileName(4);
}
}
}