the file path may or may not exist, but I need to know if it's in the right format for it to be a valid file path?
Anyone know a quick way to do this?
the file path may or may not exist, but I need to know if it's in the right format for it to be a valid file path?
Anyone know a quick way to do this?
You can use the FileInfo constructor. It will throw a ArgumentException if "The file name is empty, contains only white spaces, or contains invalid characters." It can also throw SecurityException or UnauthorizedAccessException, which I think you can ignore if you're only concerned about format.
Another option is to check against Path.GetInvalidPathChars directly. E.g.:
boolean possiblePath = pathString.IndexOfAny(Path.GetInvalidPathChars()) == -1;
Have you tried regular expressions?
^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![ ]))*(\.[a-zA-Z]{2,6})$
should work
A 100% accurate checking of a path's string format is quite difficult, since it will depend on the filesystem on which it is used (and network protocols if its not on the same computer).
Even within windows or even NTFS its not simple since it still depends on the API .NET is using in the background to communicate with the kernel.
And since most filesystems today support unicode, one might also need to check for all the rules for correcly encoded unicode, normalization, etc etc.
What I'd do is to make some basic checks only, and then handle exceptions properly once the path is used. For possible rules see:
Hi,
I found this at regexlib.com (http://regexlib.com/REDetails.aspx?regexp_id=345) by Dmitry Borysov.
"File Name Validator. Validates both UNC (\server\share\file) and regular MS path (c:\file)"
^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\/:\*\?"\|<>\. ](([^\\/:\*\?"\|<>\. ])|([^\\/:\*\?"\|<>]*[^\\/:\*\?"\|<>\. ]))?))\\)*[^\\/:\*\?"\|<>\. ](([^\\/:\*\?"\|<>\. ])|([^\\/:\*\?"\|<>]*[^\\/:\*\?"\|<>\. ]))?$
Run it with a Regex.IsMatch and you'll get a bool indicating if it's valid or not. I think regular expressions is the way to go since the file may not exist.
Here are some things you might use:
Path.IsPathRooted
to see if it's not a relative path and then use the drives from Environment.GetLogicalDrives()
to see if your path contains one of the valid drives.Path.GetInvalidFileNameChars()
and Path.GetInvalidPathChars()
which don't overlap completely. You can also use Path.GetDirectoryName(path)
and Path.GetFileName(fileName)
with your input name, which will throw an exception if The path parameter contains invalid characters, is empty, or contains only white spaces.
You cant really be sure until you try to create that file. Maybe the path is valid but security settings wont allow creation of the file. The only instance that could tell you if the path is REALLY valid would be the OS, so why dont you try to create that file an catch the IOException which indicates something went really wrong? Imho this is the easier approach: assume the input is valid and do something if it isnt, instead of doing much unnecessary work.