Get File Size Using FileSystemInfo

R

rn5a

Using the FileSystemInfo class, I am retrieving all the directories &
files existing in a particular directory on the server & listing them
in a ListBox. If an item in the ListBox happens to be a directory, then
the ListItem should display the directory name which will be appended
with the text <DIR>. If an item in the ListBox happens to be a file,
then the ListItem should display the file name which will be appended
with the size of the file. This is how I have done it:

Dim fInfo As FileInfo
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

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

For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name))
Else
fInfo = New FileInfo(Server.MapPath("Folder1\MyFile.aspx"))
lstFD.Items.Add(New ListItem(fsi.Name & " - " & fInfo.Length,
fsi.Name))
End If
Next

As such, the above code does get the correct file size (in the Else
condition) but to get the file size, I have to use the FileInfo class
additionally.

Using the FileSystemInfo class, one can get other details of a file
like when was the file created, when was it last written, when was it
last accessed etc. using properties like CreationTime, LastWriteTime,
LastAccessTime etc. respectively but there isn't any FileSystemInfo
property to get the file size (which is why I had to use the FileInfo
class).

Is there any way by which I can get the file size using the
FileSystemInfo class instead of using the FileInfo class?
 
L

Laurent Bugnion [MVP]

Hi,

Using the FileSystemInfo class, I am retrieving all the directories &
files existing in a particular directory on the server & listing them
in a ListBox. If an item in the ListBox happens to be a directory, then
the ListItem should display the directory name which will be appended
with the text <DIR>. If an item in the ListBox happens to be a file,
then the ListItem should display the file name which will be appended
with the size of the file. This is how I have done it:

Dim fInfo As FileInfo
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

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

For Each fsi In dInfo.GetFileSystemInfos

You should not do that. This gets the list of FileSystemInfos on every
loop, which will affect performances. Additionally, if a file gets added
(or worse, deleted) while your program is running, your application may
crash, or at the very least display strange results. You should get the
array of FileSystemInfos first, save it in a local variable, and then
loop using the array.
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name))
Else
fInfo = New FileInfo(Server.MapPath("Folder1\MyFile.aspx"))
lstFD.Items.Add(New ListItem(fsi.Name & " - " & fInfo.Length,
fsi.Name))
End If
Next

The easiest way to recognize if a FileSystemInfo is a FileInfo or a
DirectoryInfo is to use the "Is" keyword. Also, since the object you get
is actually a FileInfo, resp DirectoryInfo (both classes derive from
FileSystemInfo), you can simply cast, you don't need to create a new
instance:

(not totally sure about the VB.NET syntax, but I hope you get the idea)

If ( TypeOf fsi Is DirectoryInfo ) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name))
Else
fInfo = CType( fsi, FileInfo )
lstFD.Items.Add(New ListItem(fsi.Name & " - " & fInfo.Length, fsi.Name))

As such, the above code does get the correct file size (in the Else
condition) but to get the file size, I have to use the FileInfo class
additionally.

Yes, but since (with my corrections) this is the same instance, it's not
a big deal.
Using the FileSystemInfo class, one can get other details of a file
like when was the file created, when was it last written, when was it
last accessed etc. using properties like CreationTime, LastWriteTime,
LastAccessTime etc. respectively but there isn't any FileSystemInfo
property to get the file size (which is why I had to use the FileInfo
class).

Is there any way by which I can get the file size using the
FileSystemInfo class instead of using the FileInfo class?

HTH
Laurent
 
R

rn5a

Sorry, Laurent, for getting back to you after such a long time....got
engaged in some other work.
You should get the
array of FileSystemInfos first, save it in a local variable, and then
loop using the array.

Could you please show me an example of how to save the array returned
by GetFileSystemInfos in a local variable & then use the array to loop?

Thanks

Hi,

Using the FileSystemInfo class, I am retrieving all the directories &
files existing in a particular directory on the server & listing them
in a ListBox. If an item in the ListBox happens to be a directory, then
the ListItem should display the directory name which will be appended
with the text <DIR>. If an item in the ListBox happens to be a file,
then the ListItem should display the file name which will be appended
with the size of the file. This is how I have done it:

Dim fInfo As FileInfo
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

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

For Each fsi In dInfo.GetFileSystemInfos

You should not do that. This gets the list of FileSystemInfos on every
loop, which will affect performances. Additionally, if a file gets added
(or worse, deleted) while your program is running, your application may
crash, or at the very least display strange results. You should get the
array of FileSystemInfos first, save it in a local variable, and then
loop using the array.
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name))
Else
fInfo = New FileInfo(Server.MapPath("Folder1\MyFile.aspx"))
lstFD.Items.Add(New ListItem(fsi.Name & " - " & fInfo.Length,
fsi.Name))
End If
Next

The easiest way to recognize if a FileSystemInfo is a FileInfo or a
DirectoryInfo is to use the "Is" keyword. Also, since the object you get
is actually a FileInfo, resp DirectoryInfo (both classes derive from
FileSystemInfo), you can simply cast, you don't need to create a new
instance:

(not totally sure about the VB.NET syntax, but I hope you get the idea)

If ( TypeOf fsi Is DirectoryInfo ) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name))
Else
fInfo = CType( fsi, FileInfo )
lstFD.Items.Add(New ListItem(fsi.Name & " - " & fInfo.Length, fsi.Name))

As such, the above code does get the correct file size (in the Else
condition) but to get the file size, I have to use the FileInfo class
additionally.

Yes, but since (with my corrections) this is the same instance, it's not
a big deal.
Using the FileSystemInfo class, one can get other details of a file
like when was the file created, when was it last written, when was it
last accessed etc. using properties like CreationTime, LastWriteTime,
LastAccessTime etc. respectively but there isn't any FileSystemInfo
property to get the file size (which is why I had to use the FileInfo
class).

Is there any way by which I can get the file size using the
FileSystemInfo class instead of using the FileInfo class?

HTH
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top