因代码中后缀名不统一 存在大写和小写的清空,特写了一个小工具来处理这个问题
dd
成品包:
成品包.exehttps://www.aliyundrive.com/s/7zYkCK8Xaf9提取码: t23v
代码如下:[C#] 纯文本查看 复制代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Any()) {
foreach (var item in files)
{
LookFile(item);
}
}
//listBox1.Items.AddRange(files);
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else e.Effect = DragDropEffects.None;
}
/// 递归浏览所有文件,string name是你文件夹名
/// </summary>
public void LookFile(string pathname)
{
if (pathname.Trim().Length == 0)//判断文件名不为空
{
return;
}
//获取文件夹下的所有文件和文件夹
string[] files = Directory.GetFileSystemEntries(pathname);
try
{
foreach (string dir in files)
{
if (Directory.Exists(dir))//判断是否为目录,是目录继续递归
{
LookFile(dir);
}
else
{
listBox1.Items.Add(dir);//是文件的话,可以加上你要的操作
}
}
}
catch (Exception ex)
{
ex.ToString();//防止有些文件无权限访问,屏蔽异常
}
}
private void button2_Click(object sender, EventArgs e)
{
UpdateExt(false);
}
private void button1_Click(object sender, EventArgs e)
{
UpdateExt(true);
}
private void UpdateExt(bool input) {
var fileItems = listBox1.Items;
if (fileItems.Count > 0)
{
foreach (var item in fileItems)
{
string filePath = item.ToString();
// FileInfo f = new(filePath);
var ext = Path.GetExtension(filePath);
var newExt= input?ext.ToUpper():ext.ToLower();
var newPath = Path.ChangeExtension(filePath, newExt);
File.Move(filePath,newPath);
}
MessageBox.Show("修改完成!");
}
}
private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
MessageBox.Show("清除完成!");
}
} |