Checking the syntactically correct path

Is there a function in .NET that checks if a string is a syntactically correct path? I specifically don't want it to test if the path actually exists.

my current take on this is a regular expression:

([a-zA-Z]:|\\)?\\?([^/\\:*?"<>|]+[/\\])*[^/\\:*?"<>|]*

      

matches:

c:\
bbbb
\\bob/john\
..\..\

      

rejects:

xy:
c:\\bob

      

+1


source to share


3 answers


I would suggest just using a regex for this, because you specifically don't want to check if a path exists.

Here's something google helped me dig up :



RegEx="^([a-zA-Z]\:|\\\\[^\/\\:*?"<>|]+\\[^\/\\:*?"<>|]+)(\\[^\/\\:*?"<>|]+)+(\.[^\/\\:*?"<>|]+)$"

      

You could combine this with the System.IO.Path.GetInvalidPathChars () method and force the regex to dynamically exclude all invalid characters.

+1


source


I believe System.IO.Path.GetFullPath (path) throws an exception if it is not a syntactically correct path, without checking if it exists.



+3


source


Perhaps you can use System.IO.Path and the GetInvalidPathChars () function?

0


source







All Articles