William DePalo said:
I don't think that you can say definitively that a path is valid. Sure,
you can weed out some obvious invalid file paths, but the underlying file
system makes the final determination. So, what is valid on a local
NTFS-formatted disk may not be valid on a mapped network drive or on a
optical device. What is your ultimate objective, exactly?
Not the OP, but to keep this moving:
Let's assume that this is some settings dialog, which stores configuration
parameters to some service or scheduled task, so the file access attempt
won't occur until much later, but you'd like to
In that case, a good first test can be made using a simple regular
expression.
In C++/Winapi however, this type of sanitization isn't needed -- you can
just wait for the actual file access to fail. However, for
languages/libraries with "smarts" built in to file access functions
(especially i/o redirection, etc) ala perl, bash, etc, sanitization is key
because otherwise you could potentially overwrite something important.