471,582 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Get File Size Using FileSystemInfo

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?

Jan 8 '07 #1
2 8309
Hi,

rn**@rediffmail.com wrote:
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
Jan 9 '07 #2
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
Laurent Bugnion [MVP] wrote:
Hi,

rn**@rediffmail.com wrote:
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
Jan 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by wurlds_wurst_coder | last post: by
1 post views Thread by Dennis English | last post: by
1 post views Thread by Kerem Gümrükcü | last post: by
5 posts views Thread by Ram [MSFT] | last post: by
5 posts views Thread by JH | last post: by
3 posts views Thread by Steph | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

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.