【问题标题】:Count Image Files in Directory计算目录中的图像文件
【发布时间】:2014-04-17 11:18:13
【问题描述】:

我正在尝试枚举和导入文件夹中的多个图像文件。我当前用于计算图像的代码如下 -

Dim fullpath As String
fullpath = TxtPath.Text + "\"


Dim FileDirectory As New IO.DirectoryInfo(fullpath)
Dim FileJpg As IO.FileInfo() = FileDirectory.GetFiles("*.jpg")
Dim FileJpeg As IO.FileInfo() = FileDirectory.GetFiles("*.jpeg")
Dim FileGif As IO.FileInfo() = FileDirectory.GetFiles("*.gif")
Dim FileBmp As IO.FileInfo() = FileDirectory.GetFiles("*.bmp")
Dim FilePng As IO.FileInfo() = FileDirectory.GetFiles("*.png")

Dim count As Integer = 0

For Each File As IO.FileInfo In FileJpg
    count += 1
Next
For Each File As IO.FileInfo In FileJpeg
    count += 1
Next
For Each File As IO.FileInfo In FileGif
    count += 1
Next
For Each File As IO.FileInfo In FileBmp
    count += 1
Next
For Each File As IO.FileInfo In FilePng
    count += 1
Next

有没有更有效的方法在一个 For 循环中执行此操作,而不是 5 个单独的循环 - 您可以将一组文件扩展名发送到 GetFiles 吗?

我还计划使用此代码将这些图像导入数据库,因此在那里使用一个循环也会更有效率。

谢谢!

【问题讨论】:

    标签: vb.net import .net getfiles


    【解决方案1】:

    不是最好的解决方案,但我现在没有时间使用 LINQ。试试这个:

    Dim extensions As New List(Of String)
    extensions.Add("*.png")
    ' And so on, until all are in...
    
    Dim fileCount As Integer
    For i As Integer = 0 To extensions.Count - 1
      fileCount += Directory.GetFiles(txtPath.Text, extensions(i), SearchOption.AllDirectories).Length
    Next
    

    【讨论】:

      【解决方案2】:

      自从我研究 VB.NET 以来已经有一段时间了,但是在 C# 中你可以这样做:

          static void Main(string[] args)
          {
              string filePath = @"I:\Archive\2009.12.21c";
      
              List<string> extensions = new List<string>{
                  ".jpg",
                  ".jpeg",
                  ".png",
              };
      
              string[] countImages = System.IO.Directory.GetFiles(filePath);
      
              foreach (string file in countImages)
              {
                  if (extensions.Contains(System.IO.Path.GetExtension(file).ToLowerInvariant()))
                  {
                      //This is where you can add to your count for found files
                  }
              }
          }
      

      这些都是 .NET 类,应该很容易转换回 VB.NET。


      编辑@Trade

      @Trade OK,能够做到。运行你的代码(转换为 C#)和我的代码,我的 6 个循环然后是你的,然后是你的 6 个,然后是我的。你的 6 次,然后是 6 次穿过我的,然后翻转它们(所有这些都是为了确保不是 JIT 预热),结果:与我使用的方法相比,差异超过了一个数量级:

      例子:你的是 enum2,我的是 enum1

      Found 37 with dir enum1 in 609
      Found 37 with dir enum1 in 469
      Found 37 with dir enum1 in 462
      Found 37 with dir enum1 in 455
      Found 37 with dir enum1 in 448
      Found 37 with dir enum1 in 406
      Found 37 with dir enum2 in 6314
      Found 37 with dir enum2 in 6888
      Found 37 with dir enum2 in 5439
      Found 37 with dir enum2 in 5614
      Found 37 with dir enum2 in 5824
      Found 37 with dir enum2 in 6342
      

      这是您的方法的转换代码:

      private static void enum2(string filePath, List<string> extensions)
      {
          System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
          sw.Start();
          int countCount = 0;
          for (int i = 0; i < extensions.Count(); i++)
          {
              string curExt = extensions[i];
              countCount += System.IO.Directory.GetFiles(filePath, "*" +
                  curExt, System.IO.SearchOption.TopDirectoryOnly).Length;
          }
          sw.Stop();
      
          Console.WriteLine("Found {0} with dir enum2 in {1}",
              countCount, sw.ElapsedTicks.ToString());
      }
      

      【讨论】:

      • 为什么不简单地从 Directory.GetFiles 中获取 Length 属性并使用其重载来设置扩展名?另外 countImages as a string[] 可能有点令人困惑。
      • @Trade 关于 countImages 的名称你是对的,它可能是。在计数方法上(它还很早,所以大脑有点模糊)似乎会更慢,因为磁盘必须被枚举更多次。不过,我稍后会尝试停止观看这两条路线。
      • @Trade,这是否有帮助或满足任何好奇心?
      • 这很有趣,所以是的。
      猜你喜欢
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 2020-01-04
      • 1970-01-01
      • 2020-10-07
      相关资源
      最近更新 更多