|
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Globalization; |
7 | 7 | using System.IO; |
| 8 | +using System.Runtime.InteropServices; |
8 | 9 |
|
9 | 10 | using Microsoft.Extensions.FileSystemGlobbing; |
10 | 11 | using Microsoft.Extensions.FileSystemGlobbing.Abstractions; |
@@ -98,12 +99,22 @@ private Tuple<string, string> SplitFilePatternOnWildCard(string filePattern) |
98 | 99 | var splitOnWildCardIndex = filePattern.IndexOfAny(_wildCardCharacters); |
99 | 100 | var pathBeforeWildCard = filePattern.Substring(0, splitOnWildCardIndex); |
100 | 101 |
|
101 | | - // Find the last directory separator (either \ or /) before the wildcard |
102 | | - // On Windows, both \ and / are valid directory separators |
103 | | - // On Unix-like systems, only / is typically valid, but this approach is safe for both |
104 | | - var directorySeparatorIndex = Math.Max( |
105 | | - pathBeforeWildCard.LastIndexOf(Path.DirectorySeparatorChar), |
106 | | - pathBeforeWildCard.LastIndexOf(Path.AltDirectorySeparatorChar)); |
| 102 | + // Find the last directory separator before the wildcard |
| 103 | + // On Windows, we need to check both \ and / as both are valid |
| 104 | + // On Unix-like systems, only / is the directory separator |
| 105 | + int directorySeparatorIndex; |
| 106 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 107 | + { |
| 108 | + // On Windows, check both separators and take the last one found |
| 109 | + directorySeparatorIndex = Math.Max( |
| 110 | + pathBeforeWildCard.LastIndexOf(Path.DirectorySeparatorChar), |
| 111 | + pathBeforeWildCard.LastIndexOf(Path.AltDirectorySeparatorChar)); |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + // On Unix-like systems, only use the forward slash |
| 116 | + directorySeparatorIndex = pathBeforeWildCard.LastIndexOf(Path.DirectorySeparatorChar); |
| 117 | + } |
107 | 118 |
|
108 | 119 | string searchDir = filePattern.Substring(0, directorySeparatorIndex); |
109 | 120 | string pattern = filePattern.Substring(directorySeparatorIndex + 1); |
|
0 commit comments