If I have a string, I know I can use If System.IO.File.Exists(mystring) or System.IO.Directory.Exists(mystring) to see which it is. Is there a method I can use to make a single call to determine which type it is? Since, at least in Windows, you can’t have both a file and a directory with the same name, it seems like there should be a function that accepts a string and returns either “Folder”, “File”, or “Nothing”.
I’m currently doing this, but it doesn’t seem like the best way to do it:
If Directory.Exists(mystring) Then
' It's a folder
ElseIf File.Exists(mystring) Then
' It's a file
Else
' It's neither - doesn't exist
End If
Use the System.IO.File.GetAttributes()method. The returnedFileAttributesenum has a flag that indicates if it’s a directory.This works best if you know the path to exist. If the path is invalid, you will get an exception. If you don’t know that the path exists, your approach of first calling
Directory.Exists()followed byFile.Exists()is probably better.You can, of course, write your own method to wrap this logic up together, so you don’t have to repeat it in more than one place.