I'm writing code in a .NET Framework 4.7.2 app and for some reason it's not returning any files when I run this snippet on a populated directory:

var files = Directory.EnumerateFiles(appDirectory, $"{appName}.*").ToList();

The 'appDirectory' is just the path I want to enumerate and inside it are dll and exe files I am looking for (hence the filter on appName).

Now what is particularly strange to me is that I created a test console app where I could run just the method containing this snippet for test purposes and it is returning a list of files as I would expect. The test app is .NET 7.

The method signature is public static:

public static string GetVersionFromExeOrDll(string appDirectory, string appName)

The only other relevant information I can think of is in the .NET Framework app, this method is being run a little over a hundred times on 3 different threads, but the threads all access separate data and each loop within the threads are totally separate directories. When I hit a breakpoint in this method on any of the threads, I see the appropriate 'appDirectory' data and 'appName' data, but when I step through the snippet mentioned above, it is returning 0 files.

Does anyone know what could be going on here?

EDIT1: Based on comment discussions, I wanted to add some clarifications:

  1. I can clearly see a directory containing the files at the given path. I have full directory permissions to view the files and have confirmed I get the results I expect when running the method on a single test call with the exact same inputs for appDirectory and appName.
  2. The only time I don't get the results I expect is when running the method in a multi-threaded context. It appears to me that the methods Directory.EnumerateFiles() and Directory.GetFiles() both have issues when running in a multi-threaded context. I can't seem to find any documentation about this limitation...
1

There are 1 best solutions below

10
lidqy On

The filter $"{appName}.*" will find files whose name-without-extension is exactly the value of appName. So if you only have *.exe and *.dll in that folder you get two files at max returned.

I assume you want files that start with appName. So you need to add another asterisk:

var files = Directory.EnumerateFiles(appDirectory, $"{appName}*.*").ToList();