String path validation

Posted by CMAñora on Stack Overflow See other posts from Stack Overflow or by CMAñora
Published on 2012-10-18T04:58:23Z Indexed on 2012/10/18 5:00 UTC
Read the original article Hit count: 86

Filed under:
|

I have here a string(an input from the user) for a file path. I checked the string so that it will qualify the criteria:

  • check for invalid characters for a file path
  • will not accept absolute path (\Sample\text.txt)

I have tried catching the invalid characters in catch clause. It work except for '\'. It will accept 'C:\\Sample\text.txt' which is an invalid file path.

The following examples should be invalid paths:

  • :\text.txt
  • :text.txt
  • \:text.txt
  • \text.txt
  • C:\\\text.txt

I have been through similar questions posted here but none of them seemed to solve my issue.

What would be the best way to do such check?

© Stack Overflow or respective owner

Related posts about c#

Related posts about path