Connecting Tech Pros Worldwide Forums | Help | Site Map

System.IO, how to determine if valid path format

WALDO
Guest
 
Posts: n/a
#1: Nov 21 '05
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Tom Shelton
Guest
 
Posts: n/a
#2: Nov 21 '05

re: System.IO, how to determine if valid path format


In article <uH4g5ga0EHA.824@TK2MSFTNGP11.phx.gbl>, WALDO wrote:[color=blue]
> 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.
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it![/color]

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


--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 32 Days, 1 Hours, 13 Minutes, 47 Seconds
Closed Thread