通过目录中的扩展名删除特定文件 [英] Delete specific files by extension in a directory

查看:78
本文介绍了通过目录中的扩展名删除特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我知道如何删除我的目录中的所有文件:



here so far i know how to delete all files in my dir :

string[] filePaths = Directory.GetFiles(@"c:\Directory\");
foreach (string filePath in filePaths)
File.Delete(filePath);





但如何删除扩展名为.txt的文件

所以我想删除我的目录中的所有txt文件?



but how to delete file with extension .txt
so i want to delete all txt files in my dir?

推荐答案

各种各样的,



1)正如ryanb31所说,通过foreach循环检查扩展并删除它是否有。

2)用户GetFiles重载接受搜索模式过滤*。 txt文件



http://msdn.microsoft.com/en-us/library/wz42302f(v = vs.90).aspx [ ^ ]



然后全部你从getfiles方法返回应该是txt tiles。
There various was,

1) as ryanb31 said, go through the foreach loop and check for the extension and delete if it has it.
2) user the GetFiles overload that accepts a search pattern to filter by *.txt files

http://msdn.microsoft.com/en-us/library/wz42302f(v=vs.90).aspx[^]

Then all you get back from the getfiles method should be the txt tiles.


你好,



更改您的代码如下
Hello,

Change your code as follows
string[] filePaths = Directory.GetFiles(@"c:\Directory\", "*.txt");
foreach (string filePath in filePaths) {
    File.Delete(filePath);
}



可以找到更多帮助在这里 MSDN。



问候,


More help can be found here on MSDN.

Regards,


试试这个

try this
string[] Arr = null;
           Arr = Directory.GetFiles("D:\\Your Directory");
           foreach (string item in Arr)
           {
               if (item.Substring(item.LastIndexOf("."), item.Length - item.LastIndexOf(".")) == ".txt")
               {
                   File.Delete(item);
               }
           }


这篇关于通过目录中的扩展名删除特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆