且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过目录中的扩展名删除特定文件

更新时间:2022-11-28 14:52:47

各种各样的,



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);
               }
           }