将函数传递给文件的完整路径(例如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
答案 0 :(得分:6)
只需使用File.Exists,它就会接受完整路径。
编辑:抱歉,调用您的目录变量f
使我感到困惑....我相信您可以翻译以下C#代码: -
return Directory.Exists( Path.GetDirectoryName( fullPath ) );
.NET BCL ARM对这些东西有不错的报道,但我确信有更好的参考资料。 <{1}}和System.IO.Path
文档可能会很好。
答案 1 :(得分:0)
您可以使用[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