views:

201

answers:

5

Possible Duplicate:
.NET How to check if path is a file and not a directory?

Hi,

Can anyone think of a good way of telling whether a file system object path equates to a a file or a directory?

I did have the following method:

public static bool PathIsFile(this string fullPath)
{
    return (Path.GetExtension(fullPath).Length > 0);
}

But some files have no extension and some directories have a dot in their name so this is not cutting it.

Thanks

Paul

DUPILCATE http://stackoverflow.com/questions/439447/net-how-to-check-if-path-is-a-file-and-not-a-directory

A: 

There's no way of knowing just from a string analysis that something is a file or a directory, since, as you noted,

C:\WINDOWS\WhoKnowsWhatThisIs

might be either a directory or a file.

You'll have to call something like System.IO.Directory.Exists() or System.IO.File.GetAttributes() to test.

mquander
+1  A: 

Have you checked out:

System.IO.Directory.Exists(path);

and

System.IO.File.Exists(path);

These return boolean, but I can't find out (at the moment) what errors they raise if given a file or directory respectively.

There's also the System.IO.FileInfo and System.IO.DirectoryInfo classes which should help you here.

ChrisF
A: 

You have to ask the filesystem. It always knows. That is the only fool-proof way.

f1 = File(path);
bool isfile = f1.isFile();
bool isdir = f1.isDirectory();

Is an example from Java.IO.File.

Christopher
you might want to add "java" to 'Interested Tags' so you can quickly see which question is Java-related and which isn't
chakrit