1

I have a project which contains two files: book.xls and book.xlsx. If I run the following code (on .NET Framework) it finds both files as expected, despite only passing .xls as extension.

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

This is expected behavior: when you pass a three character extension to Directory.EnumerateFiles it will find all extensions which start with 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".

The strange thing is that if I run FindFiles from an xUnit project (.NET Core) it only finds 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);
        }
    }
}

Why is there a difference?

Edit 14 September 2020

It's a known issue https://github.com/dotnet/dotnet-api-docs/issues/4052

mjwills
  • 23,389
  • 6
  • 40
  • 63
Roald
  • 2,459
  • 16
  • 43
  • 2
    Are you sure you're looking at the same folder? `Directory.EnumerateFiles` isn't affected by the type of executable. Use `Path.GetAbsolutePath` to get the absolute path in the unit test – Panagiotis Kanavos Sep 10 '20 at 11:04
  • 1
    @Roald oops, [Path.GetFullPath](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getfullpath?view=netcore-3.1). You have to *prove* you're looking at the same folder though - `Directory.EnumerateFiles` doesn't change behavior when called from different applications. The only way it won't return a file is that the file isn't there or isn't visible to your account (which amounts to the same thing) – Panagiotis Kanavos Sep 10 '20 at 11:49
  • 2
    What version of .NET Framework / Core is the console app? Is the unit testing project? – mjwills Sep 10 '20 at 12:48
  • @mjwills the console app is at .NET Framework 4.6.1 and the unit test project is .NET Core 2.1 – Roald Sep 10 '20 at 12:50
  • 1
    OK, so the issue is likely core vs framework (also - you must realise you should have mentioned this earlier). Can you test the unit test on 4.6.1? And .NET Core 3.1? – mjwills Sep 10 '20 at 12:51
  • I'm not sure if a xUnit project can be converted, but I added a Unit Test Project (.NET Framework) which uses `Microsoft.VisualStudio.TestTools.UnitTesting;`. If I run the same test there it works! A bit strange because the .NET Core Microsoft documentation has the same warning about finding both the `.xls` and `.xlsx` files. A bug perhaps? – Roald Sep 10 '20 at 13:01
  • 1
    Also, to be clear you need to move your unit tests on to 4.6.1. There is no point having unit tests running **different code** to the code that the main app uses. – mjwills Sep 10 '20 at 13:03
  • @mjwills I only have Visual Studio 2017 currently, and .NET Core 3.1 is not available for that one. I didn't realize they were using different .NET Frameworks until now. Anyway I made a project with .NET Core 2.1 and ran the console code in that one. It only finds the `.xls` file. So either the documentation is not correct or it is a bug in .NET Core 2.1 – Roald Sep 10 '20 at 13:14
  • @mjwills you can add it as an answer if you want. It is a known issue. – Roald Sep 14 '20 at 07:04

1 Answers1

1

It's a known issue which is reported at https://github.com/dotnet/dotnet-api-docs/issues/4052

Roald
  • 2,459
  • 16
  • 43