Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 4
    This did not work for me. The method is not returning the clean string. It is returning the passed filename as it is. Commented Jul 17, 2013 at 6:29
  • What @Karan said, this does not work, the original string comes back. Commented Mar 20, 2014 at 15:26
  • You can actually do this with Linq like this though: var invalid = new HashSet<char>(Path.GetInvalidPathChars()); return new string(originalString.Where(s => !invalid.Contains(s)).ToArray()). Performance probably isn't great but that probably doesn't matter. Commented Jul 9, 2015 at 14:12
  • 2
    @Karan or Jon What input are you sending this function? See my edit for verification of this method. Commented Sep 24, 2015 at 2:45
  • 3
    It's easy - guys were passing strings with valid chars. Upvoted for cool Aggregate solution. Commented Jan 20, 2016 at 13:10