views:

126

answers:

2

With a function being passed a full path to a file, such as C:\someFolder\anotherFolder\someXML.xml, determine whether the folder exists. Is there a smarter/better/more elegant way of doing this? Here is my implementation:

Private Function FolderExists(ByVal fullPath As String) As Boolean
    Dim folders() As String = fullPath.Split("\")
    Dim folderPath As String = ""
    For i As Integer = 0 To folders.Length - 2 'subtract 2 to avoid appending the filename.
        folderPath += folders(i) + "\"
    Next
    Dim f As New DirectoryInfo(folderPath)
    Return f.Exists
End Function
+5  A: 

just use File.Exists instead, it accepts a full path.

EDIT: Sorry, calling your directory variable f confused me.... I trust you can translate the following C# code:-

 return Directory.Exists( Path.GetDirectoryName( fullPath ) );

The .NET BCL ARM has decent coverage of this stuff, though I'm sure there's a better reference out there. The System.IO.Path and Environment docs would probably be just fine.

Ruben Bartelink
I should have known there'd be an API call built in just for this :) Thanks!
Mark
To translate the C# code, just remove the semicolon. C# is so wordy :)
MarkJ
A: 

You can use File.Exists

Private Function FolderExists(ByVal fullPath As String) As Boolean
  return (File.exists(fullPath)
          And (File.GetAttributes(fullPath) And FileAttributes.Directory))
End Function
-1: He's got a full path including filename and needs to split it to determine if the **directory** exists and was lookign for a neat way to do that
Ruben Bartelink