Code verification: determining if a folder exists given the full path to the file?

With the function passed, the full path to the file, for example C:\someFolder\anotherFolder\someXML.xml, to determine if a folder exists. Is there a smarter / better / more elegant way to do 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
+3
source share
2 answers

just use File.Exists , it takes the full path.

EDIT: Sorry to call your directory variable fconfused me .... Hope you can translate the following C # code: -

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

.NET BCL ARM , , . System.IO.Path Environment, , .

+5

[ 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
0

Source: https://habr.com/ru/post/1724288/


All Articles