DirectoryInfo

R

rn5a

Suppose I have the following code:

<script runat="server">
Sub Page_Load(.....)
Dim dInfo As DirectoryInfo
Dim fsi As FileSystemInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))

For Each fsi In dInfo.FileSystemInfo
If (fsi.GetType Is GetType(DirectoryInfo)) Then
Response.Write(fsi.Name & " <DIR><br>")
Else
Response.Write(fsi.Name & "<FILE><br>"
End If
Next
End Sub
</script>

Assume that the folder named 'Folder1' has only 1 directory & 10 files.
When the above code is executed, then items which are directories get
successfully appended with <DIR> whereas items which are files get
appended with <FILE>. No problems till here.

But if I replace the If condition shown above (the first line within
the For..Next loop) with the following If conditiom:

If (fsi.GetType Is GetType(Directory))

i.e. change the second operand from GetType(DirectoryInfo) to
GetType(Directory), then even the single directory existing in Folder1
gets appended with <FILE> & not with <DIR>.

Why so?
 
M

Mark Fitzpatrick

The answer is the FileSystemInfo class is a base for the DirectoryInfo and
FileInfo classes. It categorizes a directory as a DirectoryInfo and not a
Directory. So, since the directory is actually of type DirectoryInfo, then
it will assume it's not a directory and get the <file> appended to it.
 
R

rn5a

Mark, to some extent, I could understand what you have said.

Now you are saying that since the directory is actually of type
DirectoryInfo, it will assume it's not a directory & hence get <FILE>
appended to it. But since the directory is actually of type
DirectoryInfo, then why doesn't it assume that it's a DirectoryInfo
instead of assuming that it's not a directory?

Could you please explain me this point? Getting a bit confused....to be
honest...

Moreover, like the FileSystemInfo class is the base class for the
DirectoryInfo & FileInfo classes, which class is the base class for the
Directory & File classes?

Thanks
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top