System.IO, how to determine if valid path format

W

WALDO

I have a small installation app that I'm building. The user can specify
an install path at the command line. How can I ensure that something
they put in is valid?

What I'm NOT looking for is determining if a path exists. I can
accomplish that with Directory.Exists(). I want them to put in a path
that may not exist and I'll create it for them.

What I AM looking for is making sure that the string is in the right
format like C:\Program Files\blah\blah\blah, not VDNSKVNKLSVNKNSVLKFNV

I suppose I could use a regular expression with no problem, but I was
wondering if there was anything in System.IO that would tell me that.


Sometimes the greatest solutions come from the simplest logic.
Being told "No" is merely the incentive to do it anyway.
 
T

Tom Shelton

I have a small installation app that I'm building. The user can specify
an install path at the command line. How can I ensure that something
they put in is valid?

What I'm NOT looking for is determining if a path exists. I can
accomplish that with Directory.Exists(). I want them to put in a path
that may not exist and I'll create it for them.

What I AM looking for is making sure that the string is in the right
format like C:\Program Files\blah\blah\blah, not VDNSKVNKLSVNKNSVLKFNV

I suppose I could use a regular expression with no problem, but I was
wondering if there was anything in System.IO that would tell me that.


Sometimes the greatest solutions come from the simplest logic.
Being told "No" is merely the incentive to do it anyway.

You could probably make use of the FolderBrowserDialog here - it lets
them create a new folder, and it would return a path...

Another way, might be to use the System.IO.Path class. If you call one
of it's methods with an illegal path (not necessarily an existing path)
it will throw an argument exception. So, you could just call it's
GetDirectoryName method which returns the directory part of the path. If
it's empty, only white space, or contains invalid characters, you'll get
an excpetion...

Function ValidPath (ByVal ThePath As String) As Boolean
Try
System.IO.Path.GetDirectoryName (ThePath)
Return True
Catch
Return False
End Try
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top