By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,612 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

How to tell if path is file or directory

P: n/a
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
}
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"Andy Gilman" <an*********@hotmail.com> wrote in message
news:OL**************@TK2MSFTNGP12.phx.gbl...
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
}

Nov 16 '05 #2

P: n/a
thanks. so a directory IS a 'file' as well as a directory

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:u7**************@tk2msftngp13.phx.gbl...
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

"Andy Gilman" <an*********@hotmail.com> wrote in message
news:OL**************@TK2MSFTNGP12.phx.gbl...
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
}


Nov 16 '05 #3

P: n/a
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

--
William Stacey, MVP

"Andy Gilman" <an*********@hotmail.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
thanks. so a directory IS a 'file' as well as a directory

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:u7**************@tk2msftngp13.phx.gbl...
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

"Andy Gilman" <an*********@hotmail.com> wrote in message
news:OL**************@TK2MSFTNGP12.phx.gbl...
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
}




Nov 16 '05 #4

P: n/a
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

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:u7**************@tk2msftngp13.phx.gbl...
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

"Andy Gilman" <an*********@hotmail.com> wrote in message
news:OL**************@TK2MSFTNGP12.phx.gbl...
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
}


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.