Path.GetInvalidPathChars() acts differently in C# interactive vs live code

This commit is contained in:
Robert McRackan 2021-10-26 13:54:37 -04:00
parent b2e956e70b
commit a3d38e082d

View File

@ -86,10 +86,15 @@ namespace FileManager
{ {
ArgumentValidator.EnsureNotNull(path, nameof(path)); ArgumentValidator.EnsureNotNull(path, nameof(path));
var invalidChars = Path.GetInvalidPathChars().Union(new[] {
'*', '?',
// these are weird. If you run Path.GetInvalidPathChars() in C# interactive, these characters are included.
// In live code, Path.GetInvalidPathChars() does not include them
'"', '<', '>'
}).ToArray();
var fixedPath = string var fixedPath = string
.Join(illegalCharacterReplacements ?? "", path.Split(Path.GetInvalidPathChars())) .Join(illegalCharacterReplacements ?? "", path.Split(invalidChars))
.Replace("*", illegalCharacterReplacements)
.Replace("?", illegalCharacterReplacements)
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); .Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
// replace all colons except within the first 2 chars // replace all colons except within the first 2 chars