.NET How to check if path is a file and not a directory?

.net, directory, file

Solution

Use:

System.IO.File.GetAttributes(string path)

and check whether the returned `FileAttributes` result contains the value `FileAttributes.Directory`:

bool isDir = (File.GetAttributes(path) & FileAttributes.Directory)
                 == FileAttributes.Directory;

Problem

I have a path and I need to determine if it is a directory or file. Is this the best way to determine if the path is a file? ``` string file = @"C:\Test\foo.txt"; bool isFile = !System.IO.Directory.Exists(file) && System.IO.File.Exists(file); ``` For a directory I would reverse the logic. ``` string directory = @"C:\Test"; bool isDirectory = System.IO.Directory.Exists(directory) && !System.IO.File.Exists(directory); ``` If both don't exists that then I won't go do either branch. So assume they both do exists.

Original source

Related problems