0

I'm downloading images from various pages. I'd like for the program to take the address of the page and filter out any letters that are not suitable for a file name (such as / or ?).

I know many of these symbols so I could just put them in a hardcoded list and remove them from the string by looping string.replace but I probably don't know all of them and googling didn't yield an "official" list of what symbols microsoft deems suitable.

Knowing .net though, I figured there is probably a function that can either fix the string or identify the bad symbols for filename purposes, is there any?

user81993
  • 6,167
  • 6
  • 32
  • 64
  • I don't have such a list on hand, but I strongly urge you to use a whitelist rather than a blacklist. That is, specify the valid characters, and allow _only_ those through, as opposed to specifying the invalid characters. – C. K. Young Jan 25 '15 at 05:49
  • 2
    Possible duplicate : http://stackoverflow.com/questions/309485/c-sharp-sanitize-file-name – dotnetstep Jan 25 '15 at 05:50

1 Answers1

0

Here is a vb.net function to remove illegal filename characters:

Private Function LegitimizeFileName(CandidateFileName As String) As String

   Return Regex.Replace(CandidateFileName, "[\\/:*?""<>|]", String.Empty)

End Function
wade
  • 35
  • 1
  • 8