public static string SanitizePath(string path, SanitizePathOptions options = SanitizePathOptions.Default) { if (options.HasFlag(SanitizePathOptions.UseEquivalentValidPathChars)) { bool inQuotes = false; return(SanitizePath(path, c => GetEquivalentValidPathChar(c, ref inQuotes), options)); } else { return(SanitizePath(path, string.Empty, options)); } }
private static string ReplaceInvalidPathChars(string path, CharReplacementEvaluatorDelegate replacementEvaluator, SanitizePathOptions options = SanitizePathOptions.Default) { string rootOrScheme = string.Empty; IEnumerable <char> invalidCharacters = Enumerable.Empty <char>(); // To match the behavior of popular web browsers, trim excess forward slashes after the scheme when using HTTP/HTTPS. // https://github.com/whatwg/url/issues/118 // This is only done when the "PreserveDirectoryStructure" flag is toggled, because otherwise the slashes are replaced anyway (as may be desired). if (options.HasFlag(SanitizePathOptions.PreserveDirectoryStructure)) { StripRepeatedForwardSlashesAfterScheme(path); } // Normalize directory separators. if (options.HasFlag(SanitizePathOptions.NormalizeDirectorySeparators)) { path = NormalizeDirectorySeparators(path); } if (options.HasFlag(SanitizePathOptions.StripInvalidPathChars)) { invalidCharacters = invalidCharacters.Concat(System.IO.Path.GetInvalidPathChars()); } if (options.HasFlag(SanitizePathOptions.StripInvalidFilenameChars)) { invalidCharacters = invalidCharacters.Concat(System.IO.Path.GetInvalidFileNameChars()); } if (options.HasFlag(SanitizePathOptions.PreserveDirectoryStructure)) { // The root of the path might contain characters that would be invalid file name characters (e.g. ':' in "C:\"). // In order to preserve the root path information, we'll remove it for now and add it back later. rootOrScheme = GetRootOrScheme(path); if (!string.IsNullOrEmpty(rootOrScheme)) { path = path.Substring(rootOrScheme.Length); } invalidCharacters = invalidCharacters.Where(c => c != System.IO.Path.DirectorySeparatorChar && c != System.IO.Path.AltDirectorySeparatorChar); } // Strip repeated directory separators. if (options.HasFlag(SanitizePathOptions.StripRepeatedDirectorySeparators)) { StripRepeatedDirectorySeparators(path); } HashSet <char> invalidCharacterLookup = new HashSet <char>(invalidCharacters); StringBuilder pathBuilder = new StringBuilder(); foreach (char c in path.ToCharArray()) { if (invalidCharacterLookup.Contains(c)) { pathBuilder.Append(replacementEvaluator(c)); } else { pathBuilder.Append(c); } } path = pathBuilder.ToString(); if (!string.IsNullOrEmpty(rootOrScheme)) { path = rootOrScheme + TrimLeftDirectorySeparators(path); } return(path); }
private static string ReplaceInvalidPathChars(string path, string replacement, SanitizePathOptions options = SanitizePathOptions.Default) { return(ReplaceInvalidPathChars(path, c => replacement, options)); }
public static string SanitizePath(string path, CharReplacementEvaluatorDelegate replacementEvaluator, SanitizePathOptions options = SanitizePathOptions.Default) { return(ReplaceInvalidPathChars(path, replacementEvaluator, options)); }
public static string SanitizePath(string path, string replacement, SanitizePathOptions options = SanitizePathOptions.Default) { return(SanitizePath(path, c => replacement, options)); }