Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I tell if its a Folder or File?

Status
Not open for further replies.

JJayUK

Programmer
Dec 18, 2001
57
GB
Is there a way to tell whether an object in a directory is a folder or a file?

I'm hoping to be able to pass in a string with the filename and get some kind of response that lets me determine if the named file is a file object or folder.

Any help appreciated,
Thanks v. much in advance. Regards,
JJayUK
 
[tt]bool isFile(string fileName) {
FileInfo fsi = new FileInfo(fileName);

if (!fsi.Exists) {
throw new FieldAccessException("Doesn't exist.");
}

if ((fsi.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
return false;
}

return true;
}[/tt]

The [tt]!fsi.Exists[/tt] test makes sure that a file or directory of the passed name exists and raises an exception if it doesn't.

The function returns false if the passed name represents a directory, and true otherwise.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top