前段时间在网上下了很多教程,文件名都是以这段字符开头“跟我一起来学XXX”,看着很是碍眼,因为文件名很长导致在文件夹里浏览的时候后面都是省略号,手动改又嫌太麻烦,有100多个文件,还是让电脑自动帮我改吧,自己动手丰衣足食,就用C#写个简单的小工具。
![](https://img7.php1.cn/3cdc5/cf6c/243/084b8e8062d35179.jpeg)
列目录函数
private void listDirectory(string path)
![](https://img7.php1.cn/3cdc5/cf6c/243/4a7ff82d461ce53e.gif)
{
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files=dir.GetFiles();
string extension=getExtension();
![](https://img7.php1.cn/3cdc5/cf6c/243/4c9420c65b39900f.gif)
fileList.Items.Clear();
![](https://img7.php1.cn/3cdc5/cf6c/243/4c9420c65b39900f.gif)
foreach (FileInfo file in files)
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
if (extension != string.Empty)
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
if (extension.IndexOf(file.Extension.ToUpper()) >= -1 && file.Extension != string.Empty)
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
fileList.Items.Add(file.Name);
}
}
else
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
fileList.Items.Add(file.Name);
}
}
} 获取扩展名
private string getExtension()
![](https://img7.php1.cn/3cdc5/cf6c/243/4a7ff82d461ce53e.gif)
{
StringBuilder sb = new StringBuilder(50);
foreach (Control control in this.groupBox1.Controls)
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
if (control.Tag.ToString().Equals("tagExtension"))
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
if ((control as CheckBox).Checked)
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
sb.Append(control.Text+";");
}
![](https://img7.php1.cn/3cdc5/cf6c/243/4c9420c65b39900f.gif)
}
}
return sb.ToString();
} 重命名方法
private void btnRename_Click(object sender, EventArgs e)
![](https://img7.php1.cn/3cdc5/cf6c/243/4a7ff82d461ce53e.gif)
{
if (MessageBox.Show("这个操作是不可逆的,确定要继续吗?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
return;
FileInfo file;
string preName = this.txtPreName.Text.Trim();
string pre=preName;
int preLen = (int)this.preLength.Value;
![](https://img7.php1.cn/3cdc5/cf6c/243/4c9420c65b39900f.gif)
try
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
for (int i &#61; 0; i < fileList.Items.Count; i&#43;&#43;)
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
file &#61; new FileInfo(fbd.SelectedPath &#43; "\\" &#43; fileList.Items[i].ToString());
![](https://img7.php1.cn/3cdc5/cf6c/243/4c9420c65b39900f.gif)
if (this.chkAuto.Checked) pre &#61; i.ToString() &#43; "." &#43; preName;
if (fileList.Items[i].ToString().Length >&#61; preLen)
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
file.MoveTo(fbd.SelectedPath &#43; "\\"&#43;pre &#43; fileList.Items[i].ToString().Substring(preLen));
}
else
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
file.MoveTo(fbd.SelectedPath &#43; "\\"&#43;pre &#43; fileList.Items[i].ToString());
}
}
MessageBox.Show("重命名完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
listDirectory(fbd.SelectedPath);
}
catch
![](https://img7.php1.cn/3cdc5/cf6c/243/df7007f7ea09f7bb.gif)
{
MessageBox.Show("重命名发生错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} RenameTool.rar