// Builds a string out of valid chars and an _ for invalid ones public static string GetValidFileName(string filename) { var invalidFileNameChars = Path.GetInvalidFileNameChars(); var pattern = new Regex("_+"); var onlyValidChars = new string(filename.Select(ch => invalidFileNameChars.Contains(ch) ? '_' : ch).ToArray()); return pattern.Replace(onlyValidChars, "_").Trim('_').Trim(); }
662400cookie-checkC# Get valid filename