using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace ChangeFileName
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fbd.SelectedPath;
}
}
/// <summary>
/// 递归算法
/// </summary>
/// <param name="strBaseDir"></param>
public void GetAllDirList(string strBaseDir)
{
DirectoryInfo di = new DirectoryInfo(strBaseDir);
DirectoryInfo[] diA = di.GetDirectories();
for (int i = 0; i < diA.Length; i++)
{
string dfileName = System.IO.Path.ChangeExtension(diA[i].FullName, ".sql");
File.Move(diA[i].FullName, dfileName);
GetAllDirList(diA[i].FullName);
//注意:递归了。
}
}
private void button2_Click(object sender, EventArgs e)
{
DirectoryInfo d = new DirectoryInfo(textBox1.Text);
GetAll(d);
MessageBox.Show("操作完成");
}
void GetAll(DirectoryInfo dir)//搜索文件夹中的文件
{
FileInfo[] allFile = dir.GetFiles();
foreach (FileInfo fi in allFile)
{
var fileName = fi.Name;
var houzui = "";
if (radioButton1.Checked)
{
houzui = fileName.Substring(fileName.LastIndexOf(".") + 1) + textBox2.Text.Trim();
}
else if (radioButton2.Checked)
{
houzui = fileName.Substring(fileName.LastIndexOf(".") + 1);
houzui=houzui.Replace(textBox2.Text.Trim(), "");
}
else if (radioButton3.Checked)
{
houzui =textBox2.Text;
}
string dfileName = System.IO.Path.ChangeExtension(fi.FullName, houzui);
File.Move(fi.FullName, dfileName);
}
DirectoryInfo[] allDir = dir.GetDirectories();
foreach (DirectoryInfo d in allDir)
{
GetAll(d);
}
}
}
}