From 229762737dcaa25e557bfa3c4103c0019f686edf Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 31 Oct 2019 11:42:23 -0700 Subject: [PATCH] Fix GetCorrectCasedPath() to first check if any system file entries is returned before calling First() (#10930) --- .../namespaces/FileSystemProvider.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 830135f516..9b2d999e57 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -155,7 +155,17 @@ namespace Microsoft.PowerShell.Commands } else { - exactPath = Directory.GetFileSystemEntries(exactPath, item).First(); + // Use GetFileSystemEntries to get the correct casing of this element + var entries = Directory.GetFileSystemEntries(exactPath, item); + if (entries.Length > 0) + { + exactPath = entries.First(); + } + else + { + // If previous call didn't return anything, something failed so we just return the path we were given + return path; + } } }