.NET Core 和 .NET Framework 之间 Directory.EnumerateFiles 的不一致行为

Inconsistent behavior of Directory.EnumerateFiles between .NET Core and .NET Framework

我有一个包含两个文件的项目:book.xlsbook.xlsx。如果我 运行 以下代码(在 .NET Framework 上)它会按预期找到两个文件,尽管只传递 .xls 作为扩展名。

using System;
using System.IO;
using System.Linq;

namespace GetFilesFromExtensionsWithTests
{
    public class Program
    {
        static void Main(string[] args)
        {
            var filesWithExtension = FindFiles("../../", "*.xls");
            foreach (string file in filesWithExtension)
            {
                Console.WriteLine($"Found: {file}");
                // Found: ../../ book.xls
                // Found: ../../ book.xlsx
            }
            Console.ReadKey();
        }

        static public string[] FindFiles(string path, string extension)
        {
            var files = Directory.EnumerateFiles(path, extension).Select(p => p).ToArray();

            return files;
        }
    }
}

这是预期的行为:当您将三个字符的扩展名传递给 Directory.EnumerateFiles 时,它将找到所有以 xls (docs):

开头的扩展名

When you use the asterisk wildcard character in a searchPattern such as "*.txt", the number of characters in the specified extension affects the search as follows:

  • If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. For example, "*.xls" returns both "book.xls" and "book.xlsx".

奇怪的是,如果我从 xUnit 项目 (.NET Core) 运行 FindFiles 它只找到 book.xls:

using GetFilesFromExtensionsWithTests;
using Xunit;

namespace GetFilesFromExtensionsWithTests_Tests
{
    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            string[] files = Program.FindFiles(
                @"..\..\..\..\FileExtensionsWithTests", "*.xls"
            );

            // Test fails, because it only finds book.xls, but not book.xlsx
            Assert.Equal(2, files.Length);
        }
    }
}

为什么会有差异?

编辑 2020 年 9 月 14 日

这是一个已知问题https://github.com/dotnet/dotnet-api-docs/issues/4052

这是一个已知问题,已在 https://github.com/dotnet/dotnet-api-docs/issues/4052

中报告