本文介绍了如何删除所有具有特定扩展名的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除所有具有特定扩展名的文件?
FileDelete(path +"* .tempfile")-在这种情况下不起作用:(

How to delete all files with specific extension?
FileDelete(path + "*.tempfile") - not helps in this case:(

推荐答案

foreach(string sFile in System.IO.Directory.GetFiles(path, "*.tempfile"))
{
    System.IO.File.Delete(sFile);
}


string[] directoryFiles = System.IO.Directory.GetFiles(path, "*.tempfile");
foreach (string directoryFile in directoryFiles)
{
   System.IO.File.Delete(directoryFile);
}


string[] filesToDelete = Directory.GetFiles("c:\\test", "*.txt");



然后,如果您使用的是.Net 3.0或更高版本,则可以使用以下代码:



Then, if you are using .Net 3.0 or higher, you can use this:

filesToDelete.ToList().ForEach(file => File.Delete(file));



或遍历数组元素并逐个删除每个项目.循环会更好,因为它可以让您处理无权删除文件或文件正在使用的情况.



Or loop through the array elements and delete each item one by one. A loop would be better since it would let you handle the cases where you do not have rights to delete a file or if the file is in use.


这篇关于如何删除所有具有特定扩展名的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:10