How to know the file name is a file or a folder in c#?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,

If I got a path like C:\testfile , how can I tell it is a file, not a
folder? This file does not have a extension name.

thanks.
 
Nicky,

You should be able to pass the path to the static Exists method on the
File class in the System.IO namespace. If the file exists, then it will
return true. Otherwise, you can pass it to the static Exists method on the
Directory class in the same namespace. This will determine if the path is a
directory.

Hope this helps.
 
I’d just hit the path/name with Directory.Exists() and File.Exists(), which
ever returns true will tell you it’s type.

Brendan
 
Nicky said:
If I got a path like C:\testfile , how can I tell it is a file, not a
folder? This file does not have a extension name.

You can create a FileInfo (or DirectoryInfo, doesn't matter) and check
the attributes.

FileInfo fileInfo = new FileInfo(@"c:\testfile");
if ((fileInfo.Attributes & FileAttributes.Directory) > 0)
... now you know it's a directory.

But of course the other answers to your question are just as valid.


Oliver Sturm
 
Back
Top