473,320 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to tell if path is file or directory

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
4 55264
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
2
by: vince | last post by:
MSDN help says you can use a UNC path for any methods that accept a path, and I'm wondering if I can also substitute an IP address for the UNC....??? Example: Using System.IO.File.Move() ...
0
by: John G | last post by:
How can I Programmatically access maximum path, file, and sum of both lengths? I am using VB.NET 2003. The PathTooLongException indicates that a path < 248 characters, filename < 260...
0
by: Ollie Riches | last post by:
I am executing a DTS programmatically from C# using COM interop. Is it possible to set the 'script file directory' from the C# code? For anyone else you gets an 'Access Denied Error' when...
0
by: Ffelagund | last post by:
There are any Path or Directory function to convert an absolute path to a relative path using a choiced directory as base to calculate the origin of the relative path?
2
by: Antonio-F100 | last post by:
Hello, I need help creating the code for a macro button on a form that will open a file directory with windows explorer. I have about 500 directories with very long names and only want to...
3
by: gazelle04 | last post by:
I wanted to create a subfolder in a directory and I used the MkDir statement but it gives me an error like "Path/File access error" Here are the codes: MkDir "\\BC123456\Attachments\" &...
4
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. Currently file-directory-related functionality in the Python standard library is scattered among various modules such as shutil, os,...
3
by: vellyna | last post by:
how to appear path file name? for example, i have select "books" from list, then i browse the file.. after browse the file, i want to appear the path file name that will save the file in a...
7
by: anupkkumar | last post by:
Hi All, Is there any way to copy a file/ directory from one location to the other? The file or the directory name should be given by the user. This is the criteria. For example:...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.