Google Search Query: how to know if a certain file is a folder in c#

Someone visited my site from google using the query: how to know if a certain file is a folder in c#

I thought I would answer it because it is not on my site and I really belive that if people come to my site they should have a chance to find what they are looking for.

Basically, there is a class called “File” in the System.IO namespace, this class exposes a lot of static methods, one of them is GetAttributes [File.GetAttributes(string pathName)]. This static method returns a FileAttributes Enumeration which in turn has a property “Directory”. If the file specified by the PathName parameter is a Directory then the enumeration can be logically tested by performing the following boolean statement.

((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory

This statement will logically [not conditionally] AND the enumeration with the Directory Attribute on the enumeration, this will leave the enumeration with only the Directory Flag set if it is already set, which can then be tested against the attribute directly. The enumeration is probably specified with the Flags attribute.

Picture of me smiling.

Paul Kinlan

Trying to make the web and developers better.

RSS Github Medium