本文介绍了如何使用Directory.GetFiles仅获取gif类型(或任何其他类型)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按扩展名过滤文件(特别是".gif"),但是出于某种原因, GetFiles 方法找不到任何文件(即使肯定存在该类型的文件)文件夹).

I'm trying to filter files by extension (in particular case ".gif") but GetFiles method for some reason does not find any files (even if there are definitely files of that type in the folder).

此代码为我提供了所有文件的列表.

This code gives me list of all files.

private void displaylastanimatedgif()
        {
            var directory = new DirectoryInfo(animatedgifsdirectory);
            var myFile = (from f in directory.GetFiles()
                          orderby f.LastWriteTime descending
                          select f).First();
            if (myFile != null)
                pictureBoxImage(myFile.FullName);
            listBox1.Items.Add(outputfile);
        }

如果我将过滤器添加到 Directory.GetFiles("*.gif")之类的 GetFiles()中,我将获得异常:

If I add filter to the GetFiles() like Directory.GetFiles("*.gif") I will get exception:

推荐答案

您有3个版本的"GetFiles"方法.其中2个包含过滤器选项: https://msdn.microsoft.com/en-us/library/system.io.directory.getfiles%28v=vs.110%29.aspx

You have 3 versions of "GetFiles" method. 2 of which includes a filter option:https://msdn.microsoft.com/en-us/library/system.io.directory.getfiles%28v=vs.110%29.aspx

您使用的版本接受路径".不是过滤器.

The version you were using accepts a "path". Not a filter.

  • Option 1 Directory.GetFiles with two arguments:

var files = Directory.GetFiles(animatedgifsdirectory, "*.gif");

  • 选项2 DirectoryInfo.GetFiles 实例方法将filter作为单个参数:

  • Option 2 DirectoryInfo.GetFiles instance method takes filter as single parameter:

    var directory = new DirectoryInfo(animatedgifsdirectory);
    var files = directory.GetFiles(animatedgifsdirectory, "*.gif");
    

  • 这篇关于如何使用Directory.GetFiles仅获取gif类型(或任何其他类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-14 07:01