代码审查:在给定完整文件路径的情况下确定文件夹是否存在?

Mar*_*ark 3 .net vb.net path-manipulation

将函数传递给文件的完整路径,例如C:\someFolder\anotherFolder\someXML.xml,确定文件夹是否存在.这样做有更聪明/更好/更优雅的方式吗?这是我的实现:

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
Run Code Online (Sandbox Code Playgroud)

Rub*_*ink 6

只需使用File.Exists,它接受一个完整的路径.

编辑:对不起,调用你的目录变量f让我困惑....我相信你可以翻译下面的C#代码: -

 return Directory.Exists( Path.GetDirectoryName( fullPath ) );
Run Code Online (Sandbox Code Playgroud)

.NET BCL ARM有这方面的东西体面的覆盖面,但我敢肯定有更好的参考那里.在System.IO.PathEnvironment文档可能会就好了.