How to tell if path is file or directory

A

Andy Gilman

I want a method basically that tells me if a path is a file or directory, or
any other possibilities.
What do i need to do? I'm hoping I dont have to manually determine all these
things with Exists()
for FileInfo and DirectoryInfo. If I do have to do that, is a directory also
a file?
Seems like something there may be built in support for but I cant find it

-simon

public PathType GetPathType (string path) {
....
determine if path is directory or file
....
}

enum PathType {
Directory,
File,
VirtualDirectory,
InvalidPath,
FileSystemUnavailable,
NonExistent
}
 
S

Shakir Hussain

Try this

// get the file attributes for file or directory
FileAttributes attr = File.GetAttributes(@"c:\Temp");

//detect whether its a directory or file
if((attr & FileAttributes.Directory) == FileAttributes.Directory)
MessageBox.Show("Its a directory");
else
MessageBox.Show("Its a file");

Shak
 
W

William Stacey [MVP]

One way to think about is like a treeview. It is a node in a tree. A node
can be of type directory or file. If a file, then the node is a leaf node
that can not contain children. If a directory, the node can contain
children. All nodes have base properties like attributes, name, etc. hth
 
A

Andy Gilman

how is it possible that i get this exception when getting attributes for a
file

Message "Could not find file \"E:\\Documents and
Settings\\Administrator\\My Documents\\testdoc.doc\"." string

StackTrace " at System.IO.__Error.WinIOError(Int32 errorCode, String
str)\r\n at System.IO.File.GetAttributes(String path)\r\n at
TSECommon.ExtractSystemIcon.GetIcon(String strPath, Boolean bSmall) in
E:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio
Projects\\StudyEdge\\TSECommon\\ExtractSystemIcon.cs:line 107" string

The documentation states that I should get -1 back if the file doesnt exist
!

-simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top