如何删除文件夹中的文件? [英] How to delete files in a folder?
本文介绍了如何删除文件夹中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,请帮忙.
如果我当前的应用程序文件夹中有一些文件,例如f1.txt,f2.txt,f3.txt,并且我想删除所有以.txt结尾的文件,该怎么办?
我知道File.Delete方法,但是如何在这里使用它呢?
Hey guys, please help.
If I have in my current app folder some files like f1.txt,f2.txt, f3.txt and I want to delete all those files ending with .txt, how can I do this?
I know about the File.Delete method, but how can i use it here?
推荐答案
您可以使用Directory.GetFiles来过滤和获取所有文件,然后您可以使用File.Delete删除.
来自MSDN的示例,
http://msdn.microsoft.com/en -us/library/system.io.file.delete.aspx?PHPSESSID = tn8k5 ... [
You can use Directory.GetFiles for filtering and getting all the files and then you can delete by using File.Delete.
sample from MSDN,
http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx?PHPSESSID=tn8k5...[^]
类似以下内容:
Something like this:
using System.IO;
string[] files = Directory.GetFiles(myPath, "*.txt", SearchOption.TopDirectoryOnly);
foreach (string file in files)
{
File.Delete(file);
}
这篇关于如何删除文件夹中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文