string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()); foreach (char c in invalid) { illegal = illegal.Replace(c.ToString(), ""); }
public string RemoveInvalidFileNameChars(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) return null; string invalid = new string(Path.GetInvalidFileNameChars()); //new string(Path.GetInvalidPathChars()); StringBuilder sb = new StringBuilder(); //foreach (char c in invalid) {fileName = fileName.Replace(c.ToString(), "");} foreach (char c in fileName) { if (!invalid.Contains(c)) sb.Append(c); } return sb.ToString(); }