Diskdrive Diskdrive - 1 month ago 16
C# Question

Figuring out whether string is valid file path?

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?

Answer

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:

Comments