From a3d38e082d76fc0187c706807934a423bd3df6d0 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Tue, 26 Oct 2021 13:54:37 -0400 Subject: [PATCH] Path.GetInvalidPathChars() acts differently in C# interactive vs live code --- FileManager/FileUtility.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/FileManager/FileUtility.cs b/FileManager/FileUtility.cs index dadf40a3..cc291b36 100644 --- a/FileManager/FileUtility.cs +++ b/FileManager/FileUtility.cs @@ -86,10 +86,15 @@ namespace FileManager { 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 - .Join(illegalCharacterReplacements ?? "", path.Split(Path.GetInvalidPathChars())) - .Replace("*", illegalCharacterReplacements) - .Replace("?", illegalCharacterReplacements) + .Join(illegalCharacterReplacements ?? "", path.Split(invalidChars)) .Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); // replace all colons except within the first 2 chars