列目录函数
private void listDirectory(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files=dir.GetFiles();
string extension=getExtension();
fileList.Items.Clear();
foreach (FileInfo file in files)
{
if (extension != string.Empty)
{
if (extension.IndexOf(file.Extension.ToUpper()) >= -1 && file.Extension != string.Empty)
{
fileList.Items.Add(file.Name);
}
}
else
{
fileList.Items.Add(file.Name);
}
}
}
{
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files=dir.GetFiles();
string extension=getExtension();
fileList.Items.Clear();
foreach (FileInfo file in files)
{
if (extension != string.Empty)
{
if (extension.IndexOf(file.Extension.ToUpper()) >= -1 && file.Extension != string.Empty)
{
fileList.Items.Add(file.Name);
}
}
else
{
fileList.Items.Add(file.Name);
}
}
}
获取扩展名
private string getExtension()
{
StringBuilder sb = new StringBuilder(50);
foreach (Control control in this.groupBox1.Controls)
{
if (control.Tag.ToString().Equals("tagExtension"))
{
if ((control as CheckBox).Checked)
{
sb.Append(control.Text+";");
}
}
}
return sb.ToString();
}
{
StringBuilder sb = new StringBuilder(50);
foreach (Control control in this.groupBox1.Controls)
{
if (control.Tag.ToString().Equals("tagExtension"))
{
if ((control as CheckBox).Checked)
{
sb.Append(control.Text+";");
}
}
}
return sb.ToString();
}
重命名方法
private void btnRename_Click(object sender, EventArgs e)
{
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;
try
{
for (int i &#61; 0; i < fileList.Items.Count; i&#43;&#43;)
{
file &#61; new FileInfo(fbd.SelectedPath &#43; "\\" &#43; fileList.Items[i].ToString());
if (this.chkAuto.Checked) pre &#61; i.ToString() &#43; "." &#43; preName;
if (fileList.Items[i].ToString().Length >&#61; preLen)
{
file.MoveTo(fbd.SelectedPath &#43; "\\"&#43;pre &#43; fileList.Items[i].ToString().Substring(preLen));
}
else
{
file.MoveTo(fbd.SelectedPath &#43; "\\"&#43;pre &#43; fileList.Items[i].ToString());
}
}
MessageBox.Show("重命名完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
listDirectory(fbd.SelectedPath);
}
catch
{
MessageBox.Show("重命名发生错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
{
if (MessageBox.Show("这个操作是不可逆的&#xff0c;确定要继续吗&#xff1f;", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) &#61;&#61; DialogResult.No)
return;
FileInfo file;
string preName &#61; this.txtPreName.Text.Trim();
string pre&#61;preName;
int preLen &#61; (int)this.preLength.Value;
try
{
for (int i &#61; 0; i < fileList.Items.Count; i&#43;&#43;)
{
file &#61; new FileInfo(fbd.SelectedPath &#43; "\\" &#43; fileList.Items[i].ToString());
if (this.chkAuto.Checked) pre &#61; i.ToString() &#43; "." &#43; preName;
if (fileList.Items[i].ToString().Length >&#61; preLen)
{
file.MoveTo(fbd.SelectedPath &#43; "\\"&#43;pre &#43; fileList.Items[i].ToString().Substring(preLen));
}
else
{
file.MoveTo(fbd.SelectedPath &#43; "\\"&#43;pre &#43; fileList.Items[i].ToString());
}
}
MessageBox.Show("重命名完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
listDirectory(fbd.SelectedPath);
}
catch
{
MessageBox.Show("重命名发生错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
RenameTool.rar