views:

251

answers:

7

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?

+5  A: 

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;
Matthew Flaschen
FWIW, this will return false positives. You'll also need to check permissions and that the folder/filename isn't one of the illegal ones ( http://en.wikipedia.org/wiki/DOS#Reserved_device_names ).
Noon Silk
A: 

Have you tried regular expressions?

^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![ ]))*(\.[a-zA-Z]{2,6})$

should work

Justin L.
He never said absolute path, there's no limit to file extension length, and that's not portable.
Matthew Flaschen
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -Jamie Zawinski
womp
+5  A: 

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:

Lawnmower
its wikipedia ;)
atamanroman
:-) thanks fielding, fixed it.
Lawnmower
A: 

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.

Jonas Stensved
A: 

The static class System.IO.Path can do wath you're asking for.

vaitrafra
+1  A: 

Here are some things you might use:

  • to check if the drive is correct (for example on one computer the drive X:\ exists, but not on yours): 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.
  • To check for valid characters, you have two methods: 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.

Rox
+1  A: 

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.

atamanroman