|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
File Path Validation Check
Hi everyone,
How do I check that a file path is valid and existing? (So that the user will not have to see a run-time error generated, in case that the specific file path is invalid / non-existant.) And if a file path is found to be invalid and non-existant, the program should allow the user to browse and select the specific file desired. (Still trying to work this out with Common Dialog control) Any ideas / suggestions on how I should go about doing this? I've been trying to think this out for almost a day now. Many thanks in advance. |
|
#2
|
|||
|
|||
|
Quote:
This will only work for local files (ie. C:\whereever\whatever.txt). If the file isn't found, it will reconstruct the path and find the last valid directory and use that in the OpenFileDialog Box. Code:
Dim InitialFilePath As String = "The initial file path to check"
Dim fso As System.IO.File
Dim direct As System.IO.Directory
Try
If fso.Exists(InitialFilePath) Then
'File is valid and exists
Else
Dim arrHold() As String
Dim strReconstructed As String
arrHold = InitialFilePath.Split("\")
If UBound(arrHold) > 0 Then
arrHold.Reverse(arrHold)
strReconstructed = arrHold(UBound(arrHold)) & "\"
If direct.Exists(strReconstructed) Then
While direct.Exists(strReconstructed & arrHold(UBound(arrHold) - 1) & "\")
strReconstructed += arrHold(UBound(arrHold) - 1) & "\"
ReDim Preserve arrHold(UBound(arrHold) - 1)
End While
OpenFileDialog1.InitialDirectory = strReconstructed
OpenFileDialog1.ShowDialog()
Else
'Root directory isn't valid
End If
Else
'Blank Value passed (should have been validated previously)
End If
End If
Catch ex As WhateverYourFavoriteExceptionsAre
Finally
fso = Nothing
direct = Nothing
End Try
(Example done in VS.Net 2003 EA) |
|
#3
|
|||
|
|||
|
Dear sevenhalo,
Thanks very much for your posting. However, I'm developing my program in VB6 and not in VB.NET. Don't think I can compile your code (posted in VB.NET) in VB6. Currently I'm trying to work something out with API for solving this problem. |
|
#4
|
|||
|
|||
|
You could try something simple like:
Code:
Private Sub Command1_Click() On Error GoTo EHandler Dim path As String path = "C:\NonExistant.NoExt" Shell path, vbNormalFocus Exit Sub EHandler: If Err.Number = "53" Then MsgBox "File Not Found!", vbOKOnly, "Error:" CommonDialog1.ShowOpen path = CommonDialog1.FileName Shell path, vbNormalFocus Else MsgBox Err.Number & " || " & Err.Description, vbOKOnly, "Error:" End If End Sub |
|
#5
|
|||
|
|||
|
Thanks for the help, Alias-Zero. I've got some ideas on how to go about with this now.
|
|
#6
|
||||
|
||||
|
Hey u could use FileSystemObject instead of this
Code:
set fs = CreateObject("Scripting.FileSystemObject")
if fs.fileexists(filename) then
msgbox "file is existing"
else
commondialog1.showopen
filename = commondialog1.filename
end if
i hope u got some idea. if not please ask. |
|
#7
|
|||
|
|||
|
Quote:
No problem. My pleasure. ![]() |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > File Path Validation Check |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|