To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Display Modes
Old Oct 5th, 2007, 12:33 PM   #1
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Resolved [RESOLVED] [2005] Validating Path

The user enters a path name into a textbox. I need to make sure this path name is valid, whether it exists or not.

The function System.IO.Path.GetInvalidFileNameChars() doesn't help with this because according to it, the colon ( is an allowable character. Unfortunately it isn't a universally allowable character.

That is: 'C:\MyFolder' is a valid folder name. However 'C:\My:Folder" is not.

While I could write code to detect for a colon in any position except character 1 in the textbox text, that seems very VB version 4. I would think that the Microsoft crew would anticipate something like this and have a built in class.

Is there one? System.IO doesn't appear to have one and this is where the Path object lives.

Anyone???
rjbudz is offline   Reply With Quote
Old Oct 5th, 2007, 12:41 PM   #2
stimbo
Frenzied Member
 
stimbo's Avatar
 
Join Date: Jun 06
Location: UK
Posts: 1,745
stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)
Re: [2005] Validating Path

vb Code:
  1. '2005 Only
  2. If My.Computer.FileSystem.DirectoryExists("Directory here") Then
  3.     'do stuff
  4. End If
  5.    'OR
  6. If IO.Directory.Exists("Directory here") Then
  7.     'do stuff
  8. End If
__________________
Stim

Free VB.NET Book Chapter
Visual Basic 2005 Cookbook Sample Chapter
stimbo is offline   Reply With Quote
Old Oct 5th, 2007, 12:55 PM   #3
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

Thank you. But the directory doesn't HAVE to exist at this point. The app will automatically create the directory later, if need be. I need to notify the user (preferably in the Text_Change event) that the path entered/changed is invalid.

If 'C:\MyFolder' is the original content of the textbox, the folder is valid.
If the user changes the text to 'C:\MyFQolder' that is valid also.
If the user changes the text to 'C:\My:Folder', that is not valid, and I want to tell them right then.

I hope that clarifies the problem.

Last edited by rjbudz; Oct 5th, 2007 at 02:16 PM.
rjbudz is offline   Reply With Quote
Old Oct 5th, 2007, 08:04 PM   #4
stimbo
Frenzied Member
 
stimbo's Avatar
 
Join Date: Jun 06
Location: UK
Posts: 1,745
stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)
Re: [2005] Validating Path

Could you perhaps use an Folder Browser Dialog instead, and when they select the folder from that, simply put the result into a textbox (making it a read only text box or whatever).

That way it will handle all the validation for you. It seems the easiest way than trying to validate a folder path in a text box that might not even exist.
__________________
Stim

Free VB.NET Book Chapter
Visual Basic 2005 Cookbook Sample Chapter
stimbo is offline   Reply With Quote
Old Oct 10th, 2007, 01:26 PM   #5
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

Even though it would be a lot easier to check for an existing folder, or allowing the user to select an existing folder, that's not how the software is going to work. I didn't get to write the spec.

The user can enter a folder name. If what they entered is a valid folder name (whether it currently exists or not!), the software creates it or accepts it, respectively.

If not, it tells the user about it.
rjbudz is offline   Reply With Quote
Old Oct 10th, 2007, 02:35 PM   #6
stimbo
Frenzied Member
 
stimbo's Avatar
 
Join Date: Jun 06
Location: UK
Posts: 1,745
stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)
Re: [2005] Validating Path

This is a horrible way of doing it but could you put the Folder in a try/catch block and simply catch any errors and alert them to the fact. It's completely barmy though
vb Code:
  1. Try
  2.       IO.Directory.CreateDirectory(Me.TextBox1.Text)
  3.    Catch ex As Exception
  4.        MessageBox.Show("The folder path you entered is not valid, try again", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
  5.         Me.TextBox1.Select()
  6. End Try
__________________
Stim

Free VB.NET Book Chapter
Visual Basic 2005 Cookbook Sample Chapter
stimbo is offline   Reply With Quote
Old Oct 10th, 2007, 02:41 PM   #7
techgnome
Snarky...
 
techgnome's Avatar
 
Join Date: May 02
Posts: 15,241
techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)
Re: [2005] Validating Path

Quote:
Originally Posted by rjbudz
Even though it would be a lot easier to check for an existing folder, or allowing the user to select an existing folder, that's not how the software is going to work. I didn't get to write the spec.

The user can enter a folder name. If what they entered is a valid folder name (whether it currently exists or not!), the software creates it or accepts it, respectively.

If not, it tells the user about it.
Specs should be able to be changed.... nothing is ever permanently set in stone... that's why we have Change Orders.

-tg
__________________
* I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
* How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
* How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
* On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
"There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
MVP '06-'10
techgnome is online now   Reply With Quote
Old Oct 11th, 2007, 11:45 AM   #8
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

I'm sorry, but you're telling me that because it's difficult to program this, then I should ask to change the specs?

I could just see the look on the face of my supervisor when I tell her we need to change the specs because it's too hard to do. I know what she would say:

lol.

Seriously though, I really thought Microsoft would anticipate this and come up with something to do it. They have thousands of classes available. How did this one get by them?

stimbo, I had thought of doing that, then deleting the directory afterward. Kind of kludge (RELLY kludge, actually! ), and I don't like to run into stuff like that when I'm doing maintenance, so I can't, in good conscience, do it.

But it would work. Thanks.

So, the answer, as I stated in the original post, will have to be to test for the colon in a substring for char position 0 and for all positions after position 1.

Not to change the specs.

I just figured if I'm going to code in .NET, I should take advantage of all it's functionality.

That's what I get for trying to be eloquent, I guess. So much for 'improved'!

I'm still convinced that they have something to do this. It's GOT to be a somewhat frequent situation. Anyone????

Last edited by rjbudz; Oct 11th, 2007 at 12:03 PM.
rjbudz is offline   Reply With Quote
Old Oct 11th, 2007, 11:58 AM   #9
stimbo
Frenzied Member
 
stimbo's Avatar
 
Join Date: Jun 06
Location: UK
Posts: 1,745
stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)
Re: [2005] Validating Path

Quote:
I'm sorry, but you're telling me that because it's difficult to program this, then I should ask to change the specs?
No, we are telling you to change the specs because there are better ways, they easily exist and you won't or rather the ridiculous specs you have been given won't allow them to be used.

Quote:
I really thought Microsoft would anticipate this and come up with something to do it.
They have - it's the FolderBrowserDialog

You are artificially creating a scenario that shouldn't be really used because of the massive risk or error that it generates. If so, I doubt they would just check for ":" and simply catch any error that could arise.

Quote:
I just figured if I'm going to code in .NET, I should take advantage of all it's functionality.
Same as the point above - use the FolderBrowserDialog

If you want to check each character in the textbox field then you could do this but this still wouldn't be correct, I can see other errors that could happen...

Code:
Dim myChar() As Char = CType(Me.TextBox1.Text, Char())

If myChar.Length > 2 Then
    For i As Integer = 2 To myChar.Length - 1
        If myChar(i) = ":" Then
            MessageBox.Show("Stop putting illegal characters in")
            Exit Sub
        End If
   Next

End If
__________________
Stim

Free VB.NET Book Chapter
Visual Basic 2005 Cookbook Sample Chapter
stimbo is offline   Reply With Quote
Old Oct 11th, 2007, 12:04 PM   #10
techgnome
Snarky...
 
techgnome's Avatar
 
Join Date: May 02
Posts: 15,241
techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)
Re: [2005] Validating Path

Plus the FolderBrowser is easier for users to use.... any time I have to type in an address like that... I fire up File Explorer, browse out to the fodler I want, copy the address and paste it where it needs to go.... anything that's more than one level deep is too much for me....

-tg
__________________
* I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
* How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
* How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
* On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
"There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
MVP '06-'10
techgnome is online now   Reply With Quote
Old Oct 11th, 2007, 12:11 PM   #11
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

Displaying an editable textbox with a preset directory path (such as 'C:\Program Files\YourNewProgram' is so common with software installation that error checking for an invalid path should be a no-brainer to include.

My thought, anyway. I guess I'm wrong, huh?
rjbudz is offline   Reply With Quote
Old Oct 11th, 2007, 12:13 PM   #12
stimbo
Frenzied Member
 
stimbo's Avatar
 
Join Date: Jun 06
Location: UK
Posts: 1,745
stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)stimbo is a jewel in the rough (200+)
Re: [2005] Validating Path

Then they would generally put the whole thing in a try/catch block as mentioned in post #6 or a mixture of validating methods. Generally not what you are trying to do.
__________________
Stim

Free VB.NET Book Chapter
Visual Basic 2005 Cookbook Sample Chapter

Last edited by stimbo; Oct 11th, 2007 at 12:16 PM.
stimbo is offline   Reply With Quote
Old Oct 11th, 2007, 12:28 PM   #13
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

I've already resigned myself to go the substring method, but just so you guys understand why the spec is laid out as it is:

The user initiates a file transfer. This is a bunch of files that have been updated or created by said user. The files are programmatically zipped up for transfer to a server (to minimize bandwidth) where they become automatically available for all the other users that need this update.

Part of the zip process is to select or create a directory predetermined by the original user (so the unzipping process will put the files in the correct place). Since these files generally go into a not-yet-created directory, the user gets to change the suggested one to anything he/she wants.

Hence the need for error checking.

Thanks, guys, for attempting to help.
rjbudz is offline   Reply With Quote
Old Oct 11th, 2007, 12:58 PM   #14
TokersBall_CDXX
Fanatic Member
 
TokersBall_CDXX's Avatar
 
Join Date: Mar 03
Location: America
Posts: 567
TokersBall_CDXX will become famous soon enough (50+)
Re: [2005] Validating Path

VB Code:
  1. Function ValidPath (ByVal ThePath As String) As Boolean
  2. Try
  3. System.IO.Path.GetDirectoryName (ThePath)
  4. Return True
  5. Catch
  6. Return False
  7. End Try
  8. End Function
__________________

There are currently users in chat!

---------------------------------

---------------------------------
TokersBall_CDXX is offline   Reply With Quote
Old Oct 11th, 2007, 02:02 PM   #15
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

? System.IO.Path.GetDirectoryName("C:\build")
returns: "C:\"

? System.IO.Path.GetDirectoryName("C:\build\some")
returns:
"C:\build"

? System.IO.Path.GetDirectoryName("C:\bu:ild\some")
returns:
"C:\bu:ild"

Unfortunately, the last doesn't cause an error, so the function will return True.

? System.IO.Path.GetDirectoryName("C:\build\so>me")
does error out, because it contains an invalid character for a directory name.

But, again, the colon ':' IS valid, just not anywhere except the second character in the string.
rjbudz is offline   Reply With Quote
Old Oct 11th, 2007, 02:52 PM   #16
bgmacaw
Fanatic Member
 
bgmacaw's Avatar
 
Join Date: Mar 07
Location: Atlanta, GA USA
Posts: 524
bgmacaw is a jewel in the rough (200+)bgmacaw is a jewel in the rough (200+)bgmacaw is a jewel in the rough (200+)
Re: [2005] Validating Path

Here's one to try to see if it will work for you

Code:
If IO.Path.IsPathRooted(newPath) Then
__________________
| My VB Notebook For .NET | My VB6 Notebook |
bgmacaw is offline   Reply With Quote
Old Oct 11th, 2007, 03:43 PM   #17
penguin5000
Addicted Member
 
Join Date: May 06
Location: Manchester, England
Posts: 255
penguin5000 is an unknown quantity at this point (<10)
Re: [2005] Validating Path

I'd be tempted to follow the spec - but as a "free of charge enhancement", put a button next to the TextBox to allow you to open the folder browser dialog, and once the user has chosen their folder, populate the TextBox with the result of the browser dialog .... and also use Stimbo's suggestion at post #6.
__________________
At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
At work - VS 2008 Standard (VB)
.NET 2.0/3.5


Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre
penguin5000 is offline   Reply With Quote
Old Oct 11th, 2007, 04:11 PM   #18
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

Quote:
Originally Posted by bgmacaw
Code:
If IO.Path.IsPathRooted(newPath) Then
Same situation. The common problem for all the methods cited is that the colon is recognized as a valid character for the path.

And it IS valid. But only as the second character.

And thanks, penguin. She's more likely to go for that than the other one!
rjbudz is offline   Reply With Quote
Old Oct 11th, 2007, 04:12 PM   #19
VBDT
PowerPoster
 
VBDT's Avatar
 
Join Date: Sep 05
Location: CA - USA
Posts: 2,908
VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)
Re: [2005] Validating Path

I think that you should use stimbo’s example in post #6. This way you would identify if the path is invalid or not. Note the user may have some other invalid characters in the path and this method will catch it. There is nothing horrible about this method and it is more efficient since it will do one instruction; create a folder. If it doesn’t succeed than caches the error so that you can notify the user if the path is entered is invalid.

Although the other solution that was mentioned above posts (using FolderBrowserDialog) would be the best, since the .Net will deal with the validation of the path. But if it can be used in your project.
VBDT is offline   Reply With Quote
Old Oct 11th, 2007, 04:16 PM   #20
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

The code in Post 6 will actually work. The folder does not have to exist on the initial user's machine however. So I will have to delete the folder one I create it.

You are quite right, DT, that the code also eliminates the need for other error checking.

But, as I expected (and see it myself) the powers that be think it lacks professionalisms, so I'll go the other less straight forward route.

Mind you, I'm not taking away from it. It will work just fine!

Last edited by rjbudz; Oct 11th, 2007 at 04:20 PM.
rjbudz is offline   Reply With Quote
Old Oct 11th, 2007, 04:17 PM   #21
techgnome
Snarky...
 
techgnome's Avatar
 
Join Date: May 02
Posts: 15,241
techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)
Re: [2005] Validating Path

Quote:
Originally Posted by rjbudz
I've already resigned myself to go the substring method, but just so you guys understand why the spec is laid out as it is:

The user initiates a file transfer. This is a bunch of files that have been updated or created by said user. The files are programmatically zipped up for transfer to a server (to minimize bandwidth) where they become automatically available for all the other users that need this update.

Part of the zip process is to select or create a directory predetermined by the original user (so the unzipping process will put the files in the correct place). Since these files generally go into a not-yet-created directory, the user gets to change the suggested one to anything he/she wants.

Hence the need for error checking.

Thanks, guys, for attempting to help.
Please note: it says nothing about allowing the user to type in the folder... just that they can CHANGE it. But hey, it's your project. Just offering up ideas.

-tg
__________________
* I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
* How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
* How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
* On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
"There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
MVP '06-'10
techgnome is online now   Reply With Quote
Old Oct 11th, 2007, 04:20 PM   #22
VBDT
PowerPoster
 
VBDT's Avatar
 
Join Date: Sep 05
Location: CA - USA
Posts: 2,908
VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)
Re: [2005] Validating Path

Quote:
So I will have to delete the folder one I create it.
Why do you want to delete the created/exiting folder?
VBDT is offline   Reply With Quote
Old Oct 11th, 2007, 04:24 PM   #23
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

techgnome, don't get me wrong. I very much appreciate everyone's input, and the diligence shown it trying to resolve the issue!

The post you quoted is the actual program as laid out by my predecessor. As such, the user can type into the textbox anything they want (though a suggested folder name is presented.
rjbudz is offline   Reply With Quote
Old Oct 11th, 2007, 04:25 PM   #24
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

DT,

Because the folder isn't for the user typing it in, but for the recipient's machine when they unzip the update (see post 13).
rjbudz is offline   Reply With Quote
Old Oct 11th, 2007, 04:26 PM   #25
bgmacaw
Fanatic Member
 
bgmacaw's Avatar
 
Join Date: Mar 07
Location: Atlanta, GA USA
Posts: 524
bgmacaw is a jewel in the rough (200+)bgmacaw is a jewel in the rough (200+)bgmacaw is a jewel in the rough (200+)
Re: [2005] Validating Path

OK, try this.

Code:
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Try
            Dim PathPattern As String = "^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?))\\)*[^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?$"
            If System.Text.RegularExpressions.Regex.IsMatch(TextBox1.Text, PathPattern) Then
                Debug.Print("True")
            Else
                Debug.Print("False")
            End If
        Catch ex As Exception
            Debug.Print("Exception")
        End Try
    End Sub
__________________
| My VB Notebook For .NET | My VB6 Notebook |
bgmacaw is offline   Reply With Quote
Old Oct 11th, 2007, 04:34 PM   #26
VBDT
PowerPoster
 
VBDT's Avatar
 
Join Date: Sep 05
Location: CA - USA
Posts: 2,908
VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)VBDT is a glorious beacon of light (400+)
Re: [2005] Validating Path

Quote:
Originally Posted by rjbudz
DT,

Because the folder isn't for the user typing it in, but for the recipient's machine when they unzip the update (see post 13).
Ok that is fine!

It is your project and you know it better. Saying that I still think that there is nothing unprofessional of using the example in post #6. Actuality I do see disadvantage of any other solutions in sense that they will deal with string manipulations that take a lot of spu time and more instructions of creating string objects, reassigning etc.
VBDT is offline   Reply With Quote
Old Oct 11th, 2007, 04:50 PM   #27
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

Yep. I agree. It does cover all the bases regarding checking a path, because if it can't create it, then it's invalid. And it's clean, terse and easy to understand.

I'm sure those that follow can utilize it.

I was looking for the .NET class that addressed the issue, but am disappointed that it doesn't exist (one of you guys would have known about it if it does, I'm sure).
rjbudz is offline   Reply With Quote
Old Oct 11th, 2007, 10:07 PM   #28
JuggaloBrotha
PowerPoster
 
JuggaloBrotha's Avatar
 
Join Date: Sep 05
Location: Lansing, MI; USA
Posts: 2,919
JuggaloBrotha is a glorious beacon of light (400+)JuggaloBrotha is a glorious beacon of light (400+)JuggaloBrotha is a glorious beacon of light (400+)JuggaloBrotha is a glorious beacon of light (400+)JuggaloBrotha is a glorious beacon of light (400+)
Re: [2005] Validating Path

bgmacaw's post using RegEx was the first thing I thought of when reading this topic.

Regex was made to cover specific things scenario's like this one. You have a need to check a string to see if it follows the windows file system path pattern. All you need to do is know what that pattern is, which is a letter followed by a colon then a backslash followed by alphanumeric characters and a slash and so on

That's a really easy pattern, and regex will match your string against that pattern returning a True or a False.
__________________
Currently using: 2008 Pro w/sp1 & VS 2010 Ultimate on Win7 Ultimate x64.


There are 3 kinds of people in the world: Those who can count and those who can't.
4 out of 3 people have trouble with fractions.

CodeBank: All ThreadsColorComboBoxFadeGradientFormVertical Tab ControlMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows
JuggaloBrotha is offline   Reply With Quote
Old Oct 12th, 2007, 10:06 AM   #29
TokersBall_CDXX
Fanatic Member
 
TokersBall_CDXX's Avatar
 
Join Date: Mar 03
Location: America
Posts: 567
TokersBall_CDXX will become famous soon enough (50+)
Re: [2005] Validating Path

hmm

how about...
VB Code:
  1. Function ValidPath(ByVal ThePath As String) As Boolean
  2.         Try
  3.             If System.IO.Path.GetFullPath(ThePath) = "" Then
  4.                 Return False
  5.             End If
  6.             Return True
  7.         Catch
  8.             Return False
  9.         End Try
  10.     End Function
__________________

There are currently users in chat!

---------------------------------

---------------------------------
TokersBall_CDXX is offline   Reply With Quote
Old Oct 12th, 2007, 10:50 AM   #30
techgnome
Snarky...
 
techgnome's Avatar
 
Join Date: May 02
Posts: 15,241
techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)techgnome has a brilliant future (2000+)
Re: [2005] Validating Path

Tokers - that only works if the path already exists... what if it doesn't?
Stimbo - that works for creating it.... what if the user then cancels? The directory is then left out there, empty.

The regEx solution posted is probably about as ideal as it's going to get.

-tg
__________________
* I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
* How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
* How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
* On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
"There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
MVP '06-'10
techgnome is online now   Reply With Quote
Old Oct 12th, 2007, 06:05 PM   #31
TokersBall_CDXX
Fanatic Member
 
TokersBall_CDXX's Avatar
 
Join Date: Mar 03
Location: America
Posts: 567
TokersBall_CDXX will become famous soon enough (50+)
Re: [2005] Validating Path

Tech,

when i test it works even when the directory does not exist.
__________________

There are currently users in chat!

---------------------------------

---------------------------------
TokersBall_CDXX is offline   Reply With Quote
Old Oct 12th, 2007, 10:14 PM   #32
iehjsucker
Addicted Member
 
iehjsucker's Avatar
 
Join Date: Sep 05
Posts: 150
iehjsucker is an unknown quantity at this point (<10)
Re: [2005] Validating Path

You make your work harder.. try this one. If the path entered, wether existing or not, is valid, you'll get true if not, then false. so easy
Code:
    Private Function ValidatePath(ByVal Path As String) As Boolean
        Try
            Dim MyPath As String
            MyPath = IO.Path.GetFullPath(Path)
            Return True
        Catch ex As Exception
            MsgBox("Path created is not valid.", MsgBoxStyle.Information, "Error")
            Return False
        End Try
    End Function
__________________





=============
..code masters..
iehjsucker is offline   Reply With Quote
Old Oct 15th, 2007, 03:31 PM   #33
rjbudz
Addicted Member
 
rjbudz's Avatar
 
Join Date: Jul 05
Location: San Diego
Posts: 252
rjbudz is an unknown quantity at this point (<10)
Re: [2005] Validating Path

Thanks for the great suggestions!
rjbudz is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:07 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.