views:

61

answers:

2

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
+3  A: 

Use the System.IO.File.GetAttributes() method. The returned FileAttributes enum has a flag that indicates if it's a directory.

string path = @"C:\Program Files";
if( (int)(File.GetAttributes( path ) & FileAttributes.Directory) != 0 )
{
   // .. 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 by File.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.

LBushkin
And if the path does not exist, as the OP suggests? You will get either a `FileNotFoundExceotion` or `DirectoryNotFoundException`. He wants a way to figure out if the _path_ itself is supposed to be one or another so he can use the corresponding `.Exists` method.
Oded
@Oded: Yes, you are correct. I was looking through MSDN for a better option, but unfortunately, one doesn't seem to exist - at least not in the built-in .NET classes. For now I've updated my post to indicate this. It may be possible to use a Win32 API call .. I'd have to look into that.
LBushkin
To be honest, I don't think there is a fool proof way to distinguish from a string if it represents a file or directory. There are too many permutations - "c:\myExe.exe" could be a funnily named directory, for example, and "c:\myDir" could be an strangely named file with no extension.
Oded
I'll wrap it in a method that returns an enum with all three values (dir, file, DNE), but I wondered if this was already done. The GetAttributes method seems messy, especially since it requires a try/catch block. Thanks for confirmation that I was going about it the "best" way.
rwmnau
A: 

Another solution might be:

if ( !string.IsNullOrEmpty( Path.GetFileName(path) ) )
{
   //it's a file
}
else if ( !string.IsNullOrEmpty( Path.GetDirectory(path) )
{
   //it's a directory
}

This is obviously not foolproof nor will it in any way establish whether the given file or directory exists on the disk. It will only determine if the path has something that looks like a filename in it. It obviously will not handle oddball situations like a directory called "foo.com". However, if you are looking for something that is pretty close that will lessen the odds of an exception, this might help.

Thomas