3

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
Machavity
  • 30,841
  • 27
  • 92
  • 100
Mark
  • 6,123
  • 13
  • 41
  • 52

2 Answers2

6

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
  • 59,778
  • 26
  • 187
  • 249
0

You can use [File.Exists](http://msdn.microsoft.com/en-us/library/system.io.file.exists(VS.71).aspx))

Private Function FolderExists(ByVal fullPath As String) As Boolean
  return (File.exists(fullPath)
          And (File.GetAttributes(fullPath) And FileAttributes.Directory))
End Function
user135810
  • 19
  • 2
  • 1
    -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 Nov 30 '09 at 14:30