Check if string is a valid path string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I check if a string represents a valid path pattern?

For example i could call Directory.Exists("BOB"); and it will tell me the
path doesnt exist. But "BOB" isnt even a valid path string.

How can i tell if a string is a valid path?
Thanks
 
Why isn't BOB a valid path string ? It may be the name of a sub-folder in
the current folder.

If you mean to check whether the path is rooted you can use
Path.IsPathRooted.

If you want to check whether the path is well formed, you can look into the
string for character belonging to Path.InvalidPathChars.
 
John,
there is no reliable way to tell whether something is a valid file path or
name apart from trying it. The reason for this is that Windows relies on the
filesystem driver (FAT, NTFS, UDFS) to determine whether something is valid
or not. There is no requirement that all filesystems follow the same rules
for valid names or path lengths.

If you want to make some assumptions on the file system then you can do a
bit better. See also

http://blogs.msdn.com/brian_dewey/archive/2004/01/19/60263.aspx

Regards
Niroo [MSFT]
 
Back
Top